标签:poi
续上一篇:在springmvc项目中使用poi导入导出excel
http://blog.csdn.net/kingson_wu/article/details/38942967
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; }
int cellCount=hssfRow.getLastCellNum();
if(-1==cellCount)
continue;//解决空行的
if(brandIdHSSFCell==null){
brandMobileInfo=null;
break;//解决不是空行但是实际上是没数据的,即为null
}
if(brandMobileInfo!=null){
brandMobileInfos.add(brandMobileInfo);
}
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; }
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); }
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://blog.csdn.net/kingson_wu/article/details/39048155