daochu
This commit is contained in:
parent
3a7c6aa4e3
commit
e4b4423b66
@ -18,6 +18,7 @@ public class CustomExcelWriteHandler implements WriteHandler {
|
||||
private final double totalTotal;
|
||||
private final int dataSize;
|
||||
private boolean totalRowCreated = false;
|
||||
private int processedDataRowCount = 0; // 已处理的数据行数
|
||||
// 缓存 Workbook(在 afterSheetCreate 中初始化)
|
||||
private Workbook workbook;
|
||||
|
||||
@ -64,82 +65,96 @@ public class CustomExcelWriteHandler implements WriteHandler {
|
||||
headRow.setHeightInPoints(25);
|
||||
}
|
||||
|
||||
// 关键:数据行写入完成后创建合计行(兼容低版本)
|
||||
// 设置数据行行高,并在最后一行数据写入后创建合计行
|
||||
public void afterRowDispose(WriteSheetHolder writeSheetHolder, Row row, Integer relativeRowIndex, Boolean isHead) throws IOException {
|
||||
// 仅在最后一行数据写入后创建合计行,且未创建过
|
||||
if (!totalRowCreated && relativeRowIndex != null && relativeRowIndex == dataSize - 1) {
|
||||
Sheet sheet = writeSheetHolder.getSheet();
|
||||
|
||||
// 修正合计行索引:0=大标题,1=表头,2~1+dataSize=数据,2+dataSize=合计
|
||||
int totalRowIndex = 2 + dataSize;
|
||||
Row totalRow = sheet.createRow(totalRowIndex);
|
||||
totalRow.setHeightInPoints(22);
|
||||
|
||||
// 合计行样式(黑底白字)
|
||||
CellStyle totalStyle = workbook.createCellStyle();
|
||||
Font totalFont = workbook.createFont();
|
||||
totalFont.setBold(true);
|
||||
totalFont.setColor(IndexedColors.WHITE.getIndex());
|
||||
totalStyle.setFont(totalFont);
|
||||
totalStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
|
||||
totalStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
totalStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
totalStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
totalStyle.setBorderTop(BorderStyle.THIN);
|
||||
totalStyle.setBorderBottom(BorderStyle.THIN);
|
||||
totalStyle.setBorderLeft(BorderStyle.THIN);
|
||||
totalStyle.setBorderRight(BorderStyle.THIN);
|
||||
|
||||
// 合并前6列写“合计”
|
||||
sheet.addMergedRegion(new CellRangeAddress(totalRowIndex, totalRowIndex, 0, 5));
|
||||
Cell totalCell = totalRow.createCell(0);
|
||||
totalCell.setCellValue("合计");
|
||||
totalCell.setCellStyle(totalStyle);
|
||||
|
||||
// 实发、个税、应发赋值+样式
|
||||
Cell actualCell = totalRow.createCell(6);
|
||||
actualCell.setCellValue(totalActual);
|
||||
actualCell.setCellStyle(totalStyle);
|
||||
|
||||
Cell taxCell = totalRow.createCell(8);
|
||||
taxCell.setCellValue(totalTax);
|
||||
taxCell.setCellStyle(totalStyle);
|
||||
|
||||
Cell totalAmtCell = totalRow.createCell(9);
|
||||
totalAmtCell.setCellValue(totalTotal);
|
||||
totalAmtCell.setCellStyle(totalStyle);
|
||||
|
||||
// 其他列补充边框
|
||||
CellStyle borderStyle = workbook.createCellStyle();
|
||||
borderStyle.setBorderBottom(BorderStyle.THIN);
|
||||
borderStyle.setBorderTop(BorderStyle.THIN);
|
||||
borderStyle.setBorderLeft(BorderStyle.THIN);
|
||||
borderStyle.setBorderRight(BorderStyle.THIN);
|
||||
borderStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
|
||||
for (int i = 0; i < 14; i++) {
|
||||
if (i == 0 || i == 6 || i == 8 || i == 9) {
|
||||
continue;
|
||||
}
|
||||
Cell cell = totalRow.getCell(i);
|
||||
if (cell == null) {
|
||||
cell = totalRow.createCell(i);
|
||||
}
|
||||
cell.setCellStyle(borderStyle);
|
||||
// 设置数据行行高(排除表头行)
|
||||
if (isHead == null || !isHead) {
|
||||
processedDataRowCount++;
|
||||
if (row.getRowNum() >= 2 && row.getRowNum() <= 1 + dataSize) {
|
||||
row.setHeightInPoints(22);
|
||||
}
|
||||
|
||||
// 标记合计行已创建
|
||||
totalRowCreated = true;
|
||||
|
||||
// 强制刷新(兼容SXSSF)
|
||||
if (sheet instanceof SXSSFSheet) {
|
||||
((SXSSFSheet) sheet).flushRows();
|
||||
|
||||
// 如果是最后一行数据,创建合计行
|
||||
if (!totalRowCreated && processedDataRowCount == dataSize && dataSize > 0) {
|
||||
createTotalRow(writeSheetHolder.getSheet());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建合计行的辅助方法
|
||||
private void createTotalRow(Sheet sheet) throws IOException {
|
||||
// 修正合计行索引:0=大标题,1=表头,2~1+dataSize=数据,2+dataSize=合计
|
||||
int totalRowIndex = 2 + dataSize;
|
||||
Row totalRow = sheet.createRow(totalRowIndex);
|
||||
totalRow.setHeightInPoints(22);
|
||||
|
||||
// 设置数据行行高
|
||||
if (row.getRowNum() >= 2 && row.getRowNum() <= 1 + dataSize) {
|
||||
row.setHeightInPoints(22);
|
||||
// 合计行样式(黑底白字)
|
||||
CellStyle totalStyle = workbook.createCellStyle();
|
||||
Font totalFont = workbook.createFont();
|
||||
totalFont.setBold(true);
|
||||
totalFont.setColor(IndexedColors.WHITE.getIndex());
|
||||
totalStyle.setFont(totalFont);
|
||||
totalStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
|
||||
totalStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||
totalStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
totalStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
totalStyle.setBorderTop(BorderStyle.THIN);
|
||||
totalStyle.setBorderBottom(BorderStyle.THIN);
|
||||
totalStyle.setBorderLeft(BorderStyle.THIN);
|
||||
totalStyle.setBorderRight(BorderStyle.THIN);
|
||||
|
||||
// 合并前6列写"合计"
|
||||
sheet.addMergedRegion(new CellRangeAddress(totalRowIndex, totalRowIndex, 0, 5));
|
||||
Cell totalCell = totalRow.createCell(0);
|
||||
totalCell.setCellValue("合计");
|
||||
totalCell.setCellStyle(totalStyle);
|
||||
|
||||
// 实发、个税、应发赋值+样式
|
||||
Cell actualCell = totalRow.createCell(6);
|
||||
actualCell.setCellValue(totalActual);
|
||||
actualCell.setCellStyle(totalStyle);
|
||||
|
||||
Cell taxCell = totalRow.createCell(8);
|
||||
taxCell.setCellValue(totalTax);
|
||||
taxCell.setCellStyle(totalStyle);
|
||||
|
||||
Cell totalAmtCell = totalRow.createCell(9);
|
||||
totalAmtCell.setCellValue(totalTotal);
|
||||
totalAmtCell.setCellStyle(totalStyle);
|
||||
|
||||
// 其他列补充边框
|
||||
CellStyle borderStyle = workbook.createCellStyle();
|
||||
borderStyle.setBorderBottom(BorderStyle.THIN);
|
||||
borderStyle.setBorderTop(BorderStyle.THIN);
|
||||
borderStyle.setBorderLeft(BorderStyle.THIN);
|
||||
borderStyle.setBorderRight(BorderStyle.THIN);
|
||||
borderStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
|
||||
for (int i = 0; i < 14; i++) {
|
||||
if (i == 0 || i == 6 || i == 8 || i == 9) {
|
||||
continue;
|
||||
}
|
||||
Cell cell = totalRow.getCell(i);
|
||||
if (cell == null) {
|
||||
cell = totalRow.createCell(i);
|
||||
}
|
||||
cell.setCellStyle(borderStyle);
|
||||
}
|
||||
|
||||
// 标记合计行已创建
|
||||
totalRowCreated = true;
|
||||
|
||||
// 强制刷新(兼容SXSSF)
|
||||
if (sheet instanceof SXSSFSheet) {
|
||||
((SXSSFSheet) sheet).flushRows();
|
||||
}
|
||||
}
|
||||
|
||||
// 备用:在所有数据写入完成后创建合计行(如果 afterRowDispose 中没有创建)
|
||||
public void afterSheetDispose(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) throws IOException {
|
||||
// 如果合计行还未创建,则创建(备用方案)
|
||||
if (!totalRowCreated && dataSize > 0) {
|
||||
createTotalRow(writeSheetHolder.getSheet());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user