50,523
社区成员
发帖
与我相关
我的任务
分享
public String createExcelByModelAndDatas(String excelModel, String excelTop, String[][] excelData, int startrow, int startcol, int endcol) throws Exception {
// 首先复制一份文件
File newfile = copyFile(excelModel,excelTop);
// 然后把数据插进去
FileInputStream fis = new FileInputStream(newfile.getAbsolutePath());
POIFSFileSystem fileSystem = new POIFSFileSystem(fis);
Workbook wb = new HSSFWorkbook(fileSystem);
Sheet sheet = wb.getSheetAt(0);
sheet.getRow(0).getCell(1).setCellValue(excelTop); // excel表头
Row curRow = null;
Cell curCell = null;
for (int i = 0; i < excelData.length; i++) { // 循环数据,依次插入
curRow = sheet.createRow(startrow + i);
for (int j = startcol; j < endcol + 1; j++) {
curCell = curRow.createCell(j);
curCell.setCellType(Cell.CELL_TYPE_STRING);
curRow.createCell(j).setCellValue(excelData[i][j]);
curCell.setCellStyle(createStyleCell(wb));
}
}
FileOutputStream fileOut = new FileOutputStream(newfile.getAbsolutePath());
fis.close();
wb.write(fileOut);
fileOut.close();
return IcAppCommon.app_excelfile_path + "/" +newfile.getName();
}
public static CellStyle createStyleCell(Workbook wb){
HSSFCellStyle cs = (HSSFCellStyle) wb.createCellStyle();
cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cs.setBottomBorderColor(HSSFColor.BLACK.index);
cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cs.setLeftBorderColor(HSSFColor.BLACK.index);
cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
cs.setRightBorderColor(HSSFColor.BLACK.index);
cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
cs.setTopBorderColor(HSSFColor.BLACK.index);
cs.setWrapText(true);
return cs;
}