您的位置:首页 > 其它

poi导入Excel,兼容03/07版本

2017-07-31 10:55 423 查看
业务背景:

由于系统需要提供给用户导入Excel文件数据的功能,但Excel文件有97-2003和2007+两种格式,且要求给用户有较大的灵活性。导入Excel文件的处理无非就是读取Excel文件的内容,然后根据一定的业务规则进行校验,校验正确后处理写入系统。对Excel文件的读取可通过JXL或POI两个Jar来完成,决定使用POI来开发,但POI对两种格式的处理又有所不同,下面案例介绍:

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下需要的jar



1.导入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) {// 2003
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
itemSortList = read2003(sheet);
} else if (version == 2007) {// 2007
XSSFWorkbook 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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: