poi导入Excel,兼容03/07版本
来源:互联网 发布:淘宝这那样下架商品 编辑:程序博客网 时间:2024/06/05 04:57
业务背景:
由于系统需要提供给用户导入Excel文件数据的功能,但Excel文件有97-2003和2007+两种格式,且要求给用户有较大的灵活性。导入Excel文件的处理无非就是读取Excel文件的内容,然后根据一定的业务规则进行校验,校验正确后处理写入系统。对Excel文件的读取可通过JXL或POI两个Jar来完成,决定使用POI来开发,但POI对两种格式的处理又有所不同,下面案例介绍:
pom.xml
1.导入Excel,先获取文件的路径,我这是使用的公司的插件获取的文件路径,在做的过程中可以直接使用Struts2的file 、fileFileName来获取文件路径(案例也就这点区别)
4.导入07
总结:03与07使用的jar不一样,在操作的过程中调用相应的jar包的数据
03版使用的是:
HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell
07版本使用的是:
XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell
pom.xml
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.13</version></dependency><dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.13</version></dependency><dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId> <version>2.6.0</version> </dependency>
lib下需要的jar1.导入Excel,先获取文件的路径,我这是使用的公司的插件获取的文件路径,在做的过程中可以直接使用Struts2的file 、fileFileName来获取文件路径(案例也就这点区别)
public void uploadExcel() {//附件String savePath ="";//文件路径获取--使用的公司的文件上传插件,先将文件保存数据库,再从附件表中查询出上传的文件获取相应的路径List<Attachment> attachmentsByAttachIds = AttachmentHelper.getAttachmentsByAttachIds(attachIds);for (int i = 0; i < attachmentsByAttachIds.size(); i++) {Attachment attachment = attachmentsByAttachIds.get(0);savePath = attachment.getSavePath();}analizeExcel(savePath);GboatAppContext.output(JsonResult.createSuccess("成功!"));}
2.根据获取的文件路径,判断文件是.xls还是.xlsx结尾的文件public void analizeExcel(String savePath) {InputStream is = null;List<ItemSort> itemSortList = null;try {version = (savePath.endsWith(".xls") ? version2003 : version2007);String savepathRoot = ConfigUtil.getSavepathRoot();String webRoot = savepathRoot + savePath;is = new FileInputStream(webRoot);if (version == 2003) {// 2003POIFSFileSystem fs = new POIFSFileSystem(is);HSSFWorkbook wb = new HSSFWorkbook(fs);HSSFSheet sheet = wb.getSheetAt(0);itemSortList = read2003(sheet);} else if (version == 2007) {// 2007XSSFWorkbook xwb = new XSSFWorkbook(is);XSSFSheet sheet = xwb.getSheetAt(0);itemSortList = read2007(sheet);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
3.导入03/** * 导入03版 * TODO * @param sheet * @return */ public List<ItemSort> read2003(HSSFSheet sheet) { List<ItemSort> userList = new ArrayList<ItemSort>(); int rowNum = sheet.getPhysicalNumberOfRows(); ItemSort ui = null; for (int i = 1; i < rowNum; i++) { HSSFRow row = sheet.getRow(i); HSSFCell c = row.getCell(0); ui = new ItemSort(); if (c != null) { //循环获取的excel表格,下面是根据自己的需求业务操作 c = row.getCell(0); if (c != null) { String itemCode=row.getCell(0).getStringCellValue(); ui.setItemCode(itemCode); if(StringUtils.isNotBlank(itemCode)) { if(itemCode.length()<3) { ui.setItemFcode(""); }else { String itemFCode = itemCode.substring(0,itemCode.length()- 2); ui.setItemFcode(itemFCode); } }else { ui.setItemFcode(""); } } else { ui.setItemCode(""); } c = row.getCell(1); if (c != null) { ui.setItemName(row.getCell(1).getStringCellValue()); } else { ui.setItemName(""); } c = row.getCell(2); if (c != null) { ui.setItemExplain(row.getCell(2).getStringCellValue()); } else { ui.setItemExplain(""); } ui.setCatalogueManageId(catalogueId); ui.setCreateDate(new Date()); itemSortBusiness.save(ui); userList.add(ui); } } return userList; }
4.导入07
/** * 导入07版 * TODO * @param sheet * @return */ public List<ItemSort> read2007(XSSFSheet sheet) { List<ItemSort> userList = new ArrayList<ItemSort>(); int rowNum = sheet.getPhysicalNumberOfRows(); ItemSort ui = null; for (int i = 1; i < rowNum; i++) { XSSFRow row = sheet.getRow(i); XSSFCell c = row.getCell(0); ui = new ItemSort(); if (c != null) { c = row.getCell(0); if (c != null) { String itemCode=row.getCell(0).getStringCellValue(); ui.setItemCode(itemCode); if(StringUtils.isNotBlank(itemCode)) { if(itemCode.length()<3) { ui.setItemFcode(""); }else { String itemFCode = itemCode.substring(0,itemCode.length()- 2); ui.setItemFcode(itemFCode); } }else { ui.setItemFcode(""); } } else { ui.setItemCode(""); } c = row.getCell(1); if (c != null) { ui.setItemName(row.getCell(1).getStringCellValue()); } else { ui.setItemName(""); } // // c = row.getCell(2); if (c != null) { ui.setItemExplain(row.getCell(2).getStringCellValue()); } else { ui.setItemExplain(""); } ui.setCatalogueManageId(catalogueId); itemSortBusiness.save(ui); userList.add(ui); } } return userList; }
总结:03与07使用的jar不一样,在操作的过程中调用相应的jar包的数据
03版使用的是:
HSSFWorkbook、HSSFSheet、HSSFRow、HSSFCell
07版本使用的是:
XSSFWorkbook、XSSFSheet、XSSFRow、XSSFCell
阅读全文
0 0
- poi导入Excel,兼容03/07版本
- POI导出Excel 兼容03,07
- ssh框架POi导入excel表格兼容excel2003和2007版本
- java poi 读取excel(兼容03,07)
- poi解决excel 03 与 07不兼容的问题
- 《poi excel批量导入数据库 2003与2007兼容》使用说明
- Jtable导入Excel数据使用poi兼容exls格式
- poi读写Excel文件,兼容新旧版本的简单例子
- poi 读取excel(03和07版本)--小结
- poi 导入excel源码
- POI导入excel出错
- 使用POI 导入excel
- POI导入Excel
- excel导入(poi)
- poi导入导出excel
- poi excel导入导出
- poi 导入 Excel
- POI实现excel导入
- 为laravel5.1生产环境linux从源代码安装PHP
- Android 4.4 Graphic 图形架构
- 我的第一篇博客
- Java代理
- 类模板以及其中的traits技术和type classification技术
- poi导入Excel,兼容03/07版本
- laravel下的团队开发
- laravel重要概念和知识点
- PHP的模板引擎这点事儿
- Hadoop 资源调度架构和实现
- laravel, Composer和autoloading
- struts2前台后台逻辑串联
- PHPStorm/webstorm tips
- gulp some tips