Java上传Excel并解析
来源:互联网 发布:sql分页查询语句join 编辑:程序博客网 时间:2024/06/05 00:53
文件上传
public String uploadFile(CommonsMultipartFile file,String uploadPath,String realUploadPath){InputStream is = null;OutputStream os = null;Calendar calendar = Calendar.getInstance();//获取时间long excelName = calendar.getTime().getTime();try {is = file.getInputStream();String des = realUploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();os = new FileOutputStream(des);byte[] buffer = new byte[1024];int len = 0;while((len = is.read(buffer))>0){os.write(buffer);}} catch (Exception e) {e.printStackTrace();}finally{if(is!=null){try{is.close();}catch (Exception e2){e2.printStackTrace();}}if(os!=null){try{os.close();}catch (Exception e2){e2.printStackTrace();}}}//返回路径return uploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();}
常用的Excel解析方式有两种JXL,POI
jxl用起来相对简单,但只支持Excel2003版本,也就是说jxl无法解析.xlsx的Excel文件,而POI会识别Excel版本进行解析,所以大部分人更倾向于POI
jxl使用时需要在项目中导入jxl.jar包
poi需要导入
poi-3.14-20160307.jar
poi-ooxml-3.14-20160307.jar
poi-ooxml-schemas-3.14-20160307.jar
commons-io-1.4.jar
commons-fileupload-1.2.1.jar
jxl解析代码
public String readExcel(CommonsMultipartFile file,HttpServletRequest request)throws IOException, WriteException{StringBuffer sb = new StringBuffer();//将读取的内容存入StringBUffer中try {Workbook book = Workbook.getWorkbook(file.getInputStream());try{ Sheet sheet = book.getSheet(0); for(int i = 0 ; i < 3 ; i++){//i表示行数 for(int j = 0 ; j < 4 ; j++){//j表示列数 sb.append(sheet.getCell(j, i).getContents()+"\t"); } sb.append("\n"); } System.out.println(sb); }finally{ if(book != null){ book.close(); } }} catch (BiffException e) { System.err.println(e+""); } catch (IOException e) { System.err.println(e+"文件读取错误"); }return "";}poi代码
private POIFSFileSystem fs; private HSSFWorkbook wb; private HSSFSheet sheet; private HSSFRow row; /*读取标题excel第一行内容*/public String[] readExcelTitle(InputStream is) { try { fs = new POIFSFileSystem(is); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); row = sheet.getRow(0); // 标题总列数 int colNum = row.getPhysicalNumberOfCells(); System.out.println("colNum:" + colNum); String[] title = new String[colNum]; for (int i = 0; i < colNum; i++) { //title[i] = getStringCellValue(row.getCell((short) i)); title[i] = getCellFormatValue(row.getCell((short) i)); } return title; }/*读取内容*/ public void readExcelContent(InputStream is) { Map<Integer, ModelCourse> content = new HashMap<Integer, ModelCourse>(); ModelCourse model=new ModelCourse(); try { fs = new POIFSFileSystem(is); wb = new HSSFWorkbook(fs); } catch (IOException e) { e.printStackTrace(); } sheet = wb.getSheetAt(0); // 得到总行数 int rowNum = sheet.getLastRowNum(); row = sheet.getRow(0); int colNum = row.getPhysicalNumberOfCells(); // 正文内容从第二行开始,第一行为表头的标题 for (int i = 1; i <=rowNum; i++) { row = sheet.getRow(i); int j = 0; while (j < colNum) { if(j==1){ model.setCourse_id(getCellFormatValue(row.getCell((short) j))); } else if(j==2){ model.setCourse_name(getCellFormatValue(row.getCell((short) j))); } else if(j==3){ model.setCourse_time(getCellFormatValue(row.getCell((short) j))); } else if(j==4){ model.setCourse_place(getCellFormatValue(row.getCell((short) j))); } j++; } content.put(i, model); addCourse(model); } }
阅读全文
0 0
- Java上传Excel并解析
- Excel上传并解析(java)
- 上传并解析excel文件
- Java上传资源excel文件并进行解析
- springmvc上传excel并解析excel
- java上传excel并读取excel
- node.js上传并解析Excel文件
- node.js上传并解析Excel文件
- struts2--实现Excel上传并解析
- java 解析excel并输出
- Extjs4.0上传并解析excel和导出excel
- Extjs4.0上传并解析excel和导出excel
- java实现excel文件上传并解析内容保存到数据库中
- JAVA JXL -----excel文件上传和解析
- java web:excel文件上传与解析
- java/excel上传,解析,导入数据库
- java实现excel文件上传,解析,导入
- struts2上传excel文件并解析数据插入数据库
- SourceTree本地分支误删解决办法
- 集成学习(Ensemble Learning)
- ODF光纤配线架
- Camera---获得摄像机的视口区域(转载大神雨松MOMO)
- Educational Codeforces Round 26 A. Text Volume
- Java上传Excel并解析
- 简单布隆过滤器实现、布隆过滤器扩展
- [转]深度学习如何入门?
- HDU 1892 See you~(二维树状数组 单点更新)
- window下eclipse ndk配置
- Kail Linux fluxion WiFi破解
- 百度传课-php2小时超音速入门
- 在大学老师布置的一些题目,欢迎各位大佬指正(6)
- 大数据学习笔记:编写Mapreduce代码并运行