续上一篇:在springmvc项目中使用poi导入导出excel
http://blog.csdn.net/kingson_wu/article/details/38942967
一.
导入时,发现了不少问题,如果是导出excel之后,在里面不删除行,只是简单的修改一些数据的话,则不会出问题,但如果是删除了一些行,或者excel表不是导出的,而是另外的excel文件,里面有很多数据ctrl+a,ctrl+v生成的,那么导入的时候就会出问题,因为里面虽然看起来的数据就那么多,但是有一些数据痕迹。很多行是空白的但是在导入的时候代码并不会认为它是空的,这样就会把空行的值转成数据导致出错。
先上一段解决了这个bug的代码:
private List<BrandMobileInfoEntity> readBrandPeriodSorXls(InputStream is) throws IOException, ParseException { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); List<BrandMobileInfoEntity> brandMobileInfos = new ArrayList<BrandMobileInfoEntity>(); BrandMobileInfoEntity brandMobileInfo = null; // 循环工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // 循环行Row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); int cellCount=hssfRow.getLastCellNum(); if(-1==cellCount) continue;//解决空行的 brandMobileInfo = new BrandMobileInfoEntity(); for (int i = 0; i < cellCount; i++) { HSSFCell brandIdHSSFCell = hssfRow.getCell(i); if(brandIdHSSFCell==null){ brandMobileInfo=null; break;//解决不是空行但是实际上是没数据的,即为null } if (i == 0) { //System.out.println("=================="+getCellValue(brandIdHSSFCell)); if(getCellValue(brandIdHSSFCell)==null||!StringUtils.isNumeric(getCellValue(brandIdHSSFCell))||"".equals(getCellValue(brandIdHSSFCell))){ i=17; continue;//第一列不合法,整行都不读取 }else{ brandMobileInfo.setBrandId(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 1) { continue; } else if (i == 2) { continue; } else if (i == 3) { continue; } else if (i == 4) { continue; } else if (i == 5) { brandMobileInfo.setWarehouse(getCellValue(brandIdHSSFCell)); } else if (i == 6) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setSortA1(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 7) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setSortA2(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 8) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setSortB(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 9) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setSortC10(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 10) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setSortC(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } else if (i == 11) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&getCellValue(brandIdHSSFCell).length()<9) brandMobileInfo.setHitA(getCellValue(brandIdHSSFCell)); } else if (i == 12) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&getCellValue(brandIdHSSFCell).length()<9) brandMobileInfo.setHitB(getCellValue(brandIdHSSFCell)); } else if (i == 13) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&getCellValue(brandIdHSSFCell).length()<9) brandMobileInfo.setHitC(getCellValue(brandIdHSSFCell)); } else if (i == 14) { String customSellType=getCellValue(brandIdHSSFCell); if("今日上新".equals(customSellType)){ customSellType="today"; }else if("正在热卖".equals(customSellType)){ customSellType="yesterday"; }else if("最后机会".equals(customSellType)){ customSellType="lastday"; }else { customSellType="no_defined"; } brandMobileInfo.setCustomSellType(customSellType); }else if (i == 15) { continue; }else if (i == 16) { if(StringUtils.isNumeric(getCellValue(brandIdHSSFCell))&&!"".equals(getCellValue(brandIdHSSFCell))){ brandMobileInfo.setChannelId(Integer.parseInt(getCellValue(brandIdHSSFCell))); } } } if(brandMobileInfo!=null){ brandMobileInfos.add(brandMobileInfo); } } } return brandMobileInfos; }
这段代码中,有两段是解决这个bug的
(1)
int cellCount=hssfRow.getLastCellNum();
if(-1==cellCount)
continue;//解决空行的
是空行,就是说这一行的数据一列都没有,返回-1,这种情况是绝对没有数据的,要跳出。
(2)
if(brandIdHSSFCell==null){
brandMobileInfo=null;
break;//解决不是空行但是实际上是没数据的,即为null
}
if(brandMobileInfo!=null){
brandMobileInfos.add(brandMobileInfo);
}
这一种是不是空行,这一行是有一定列数的,但是里面的单元格确实没有数据的,是null的,这种也要跳出。
二.导出时,表格进行一些优化。
(1)在excel表中生成下拉框。
public static HSSFSheet setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) { // 加载下拉列表内容 DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist); // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列 CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow, firstCol, endCol); // 数据有效性对象 HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint); sheet.addValidationData(data_validation_list); return sheet; }
(2)数字填充在表格设置居中并且左上角没有小三角形
CellUtil.setAlignment(row.createCell(j), wb, CellStyle.ALIGN_CENTER); insertDataCell(row, j++, brandCompleteInfo.getSortA1()); CellUtil.setAlignment(row.createCell(j), wb, CellStyle.ALIGN_CENTER); insertDataCell(row, j++, brandCompleteInfo.getSortA2());
private void insertDataCell(HSSFRow row,int i,int object){ row.getCell(i).setCellValue(object); }
(3)对某些列设置合适的宽度
sheet.autoSizeColumn((short)0); //adjust width of the first column //sheet.autoSizeColumn((short)1); //adjust width of the second column sheet.setColumnWidth(1, 14000); sheet.autoSizeColumn((short)2); sheet.autoSizeColumn((short)3); sheet.setColumnWidth((short)4, 3000); sheet.autoSizeColumn((short)11); sheet.autoSizeColumn((short)12); sheet.autoSizeColumn((short)13); sheet.setColumnWidth((short)14, 3500);
更多的操作可以参考poi的官方文档:http://poi.apache.org/spreadsheet/quick-guide.html
时间: 2024-10-12 20:12:28