日期:2014-05-16 浏览次数:20786 次
try { is = new FileInputStream(file); HSSFWorkbook wb=null; try { //office 2003和以前版本 wb = new HSSFWorkbook(new POIFSFileSystem(is)); } catch (RuntimeException e) { //office 2007版本 XSSFWorkbook wb1 = new XSSFWorkbook(file.getPath()); return import2007(wb1); } //工作表数 int sheetNum = wb.getNumberOfSheets(); for (int i = 0; i < sheetNum ; i++) { HSSFSheet childSheet = wb.getSheetAt(i); int rowNum = childSheet.getLastRowNum(); for (int j = childSheet.getFirstRowNum(); j < rowNum; j++) { HSSFRow row = childSheet.getRow(j); if(row!=null){ int cellNum = row.getLastCellNum(); String[] str=new String[cellNum]; for (int k = 0; k < cellNum; k++) { if(row.getCell(k)!=null){ str[k]=row.getCell(k).toString(); }else{ str[k]=null; } } } } } }
//2007版本excel public String import2007(XSSFWorkbook wb){ StockderDAO dao=(StockderDAO)getDao(); int sheetNum = wb.getNumberOfSheets(); for (int i = 0; i <sheetNum ; i++) { XSSFSheet childSheet = wb.getSheetAt(i); int rowNum = childSheet.getLastRowNum(); for (int j = childSheet.getFirstRowNum(); j < rowNum; j++) { XSSFRow row = childSheet.getRow(j); int cellNum = row.getLastCellNum(); String[] str=new String[cellNum]; for (int k = 0; k < cellNum; k++) { if(row.getCell(k)!=null){ str[k]=row.getCell(k).toString(); }else{ str[k]=null; } } if(j!=0){ boolean flag=dao.getBuyDistinct(str,this.request.getParameterMap()); if(flag){ XSSFCell cel=row.createCell(cellNum ); cel.setCellValue("R"); } } } } ServletOutputStream fOut=null; try { fOut=response.getOutputStream(); wb.write( fOut); fOut.flush(); fOut.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return NONE; }