org.apache.poi 原理就是遍历sheet页的所有单元格,没有单元格的需要生成空白单元格(针对那些跨行跨列的),来生成样式。
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row sheetAtRow = sheet.getRow(i);
if (sheetAtRow == null) {
sheetAtRow = sheet.createRow(i);
}
for (int j = 0; j < 4; j++) {
Cell cell39 = sheetAtRow.getCell(j);
if (cell39 == null) {
cell39 = sheetAtRow.createCell(j);
}
CellStyle cellStyle5 = cell39.getCellStyle();
if (cellStyle5 == null) {
cellStyle5 = workbook.createCellStyle();
}
cellStyle5.setBorderBottom(BorderStyle.THIN);
cellStyle5.setBorderTop(BorderStyle.THIN);
cellStyle5.setBorderLeft(BorderStyle.THIN);
cellStyle5.setBorderRight(BorderStyle.THIN);
cellStyle5.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle5.setTopBorderColor(IndexedColors.BLACK.getIndex());
cellStyle5.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyle5.setRightBorderColor(IndexedColors.BLACK.getIndex());
cell39.setCellStyle(cellStyle5);
}
}