POI插入多个sheet
今天需要做一个导出excel的功能,还是使用POI进行导入,但是要导出一个excel中的多个sheet中。所以就做下标记咯。FileOutputStream fileOut = null;
FileInputStream fileIn = null;
fileIn = new FileInputStream(new File(sourceFilePath));
fileOut=new FileOutputStream(new File(localFilePath));
HSSFWorkbook wbIn = null;
wbIn=new HSSFWorkbook(fileIn);
HSSFCellStyle cellStyle=wbIn.createCellStyle();
HSSFFont font = wbIn.createFont(); //设置字体的样式
font.setFontHeightInPoints((short)10); //字体大小
cellStyle.setFont(font);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFSheet sheet=null;
HSSFRow row=null;
for(int i=0,j=ls.size();i<j;i++){
if(i==0){
sheet = wbIn.getSheet("发货汇总");
}else if(i==1){
sheet = wbIn.getSheet("根据出厂时间统计每月返厂数量");
}else{
sheet = wbIn.getSheet("根据返厂时间统计每月返厂数量");
}
ResultCollection rc=ls.get(i);
for(int k=0,l=rc.getRowSize();k<l;k++){
row=sheet.createRow((short)k+1);
row.createCell(0).setCellValue(k);
row.createCell(1).setCellValue(Integer.parseInt(rc.getCell(k, 0)));
row.createCell(2).setCellValue(new HSSFRichTextString(rc.getCell(k, 1)));
row.createCell(3).setCellValue(new HSSFRichTextString(rc.getCell(k, 2)));
if(i>0){
row.createCell(4).setCellValue(new HSSFRichTextString(rc.getCell(k, 4)));
row.createCell(5).setCellValue(Integer.valueOf(rc.getCell(k, 3)));
row.getCell(5).setCellStyle(cellStyle);
}else{
row.createCell(4).setCellValue(Integer.valueOf(rc.getCell(k, 3)));
}
row.getCell(0).setCellStyle(cellStyle);
row.getCell(1).setCellStyle(cellStyle);
row.getCell(2).setCellStyle(cellStyle);
row.getCell(3).setCellStyle(cellStyle);
row.getCell(4).setCellStyle(cellStyle);
}
}
wbIn.write(fileOut);
fileOut.close();
fileIn.close();