修正报表

This commit is contained in:
2026-04-25 22:57:16 +08:00
parent 09c3e438e2
commit fb1b412508

View File

@@ -16,9 +16,24 @@ import org.springframework.web.bind.annotation.*;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
import org.apache.poi.ss.usermodel.ComparisonOperator;
import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
import org.apache.poi.ss.usermodel.FontFormatting;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDate; import java.time.LocalDate;
@@ -414,34 +429,119 @@ public class LbDailyUserTradeReportController {
String d0 = parsedReportDate.format(mmdd); String d0 = parsedReportDate.format(mmdd);
String d1 = parsedReportDate.minusDays(1).format(mmdd); String d1 = parsedReportDate.minusDays(1).format(mmdd);
float rowHeightFactor = 1.3f;
sheet.setDefaultRowHeightInPoints(sheet.getDefaultRowHeightInPoints() * rowHeightFactor);
DataFormat dataFormat = workbook.createDataFormat();
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.DARK_BLUE.getIndex());
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headerStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
CellStyle textStyle = workbook.createCellStyle();
textStyle.setAlignment(HorizontalAlignment.LEFT);
textStyle.setVerticalAlignment(VerticalAlignment.CENTER);
textStyle.setBorderTop(BorderStyle.THIN);
textStyle.setBorderBottom(BorderStyle.THIN);
textStyle.setBorderLeft(BorderStyle.THIN);
textStyle.setBorderRight(BorderStyle.THIN);
CellStyle zebraTextStyle = workbook.createCellStyle();
zebraTextStyle.cloneStyleFrom(textStyle);
zebraTextStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
zebraTextStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle intStyle = workbook.createCellStyle();
intStyle.setAlignment(HorizontalAlignment.RIGHT);
intStyle.setVerticalAlignment(VerticalAlignment.CENTER);
intStyle.setDataFormat(dataFormat.getFormat("#,##0"));
intStyle.setBorderTop(BorderStyle.THIN);
intStyle.setBorderBottom(BorderStyle.THIN);
intStyle.setBorderLeft(BorderStyle.THIN);
intStyle.setBorderRight(BorderStyle.THIN);
CellStyle zebraIntStyle = workbook.createCellStyle();
zebraIntStyle.cloneStyleFrom(intStyle);
zebraIntStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
zebraIntStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
CellStyle summaryTextStyle = workbook.createCellStyle();
summaryTextStyle.cloneStyleFrom(textStyle);
summaryTextStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
summaryTextStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font summaryFont = workbook.createFont();
summaryFont.setBold(true);
summaryTextStyle.setFont(summaryFont);
summaryTextStyle.setBorderTop(BorderStyle.THIN);
CellStyle summaryIntStyle = workbook.createCellStyle();
summaryIntStyle.cloneStyleFrom(intStyle);
summaryIntStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
summaryIntStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
summaryIntStyle.setFont(summaryFont);
summaryIntStyle.setBorderTop(BorderStyle.THIN);
int r = 0; int r = 0;
Row header = sheet.createRow(r++); Row header = sheet.createRow(r++);
header.createCell(0).setCellValue("昵称"); header.setHeightInPoints(20 * rowHeightFactor);
header.createCell(1).setCellValue(d1 + "买货"); createHeaderCell(header, 0, "昵称", headerStyle);
header.createCell(2).setCellValue(d0 + ""); createHeaderCell(header, 1, d1 + "", headerStyle);
header.createCell(3).setCellValue(d0 + ""); createHeaderCell(header, 2, d0 + "", headerStyle);
header.createCell(4).setCellValue("服务费"); createHeaderCell(header, 3, d0 + "买货", headerStyle);
header.createCell(5).setCellValue("差额"); createHeaderCell(header, 4, "服务费", headerStyle);
header.createCell(6).setCellValue("应收应付"); createHeaderCell(header, 5, "差额", headerStyle);
header.createCell(7).setCellValue("抵扣金额"); createHeaderCell(header, 6, "应收应付", headerStyle);
createHeaderCell(header, 7, "抵扣金额", headerStyle);
sheet.createFreezePane(0, 1);
sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, 7));
for (LbDailyUserTradeReport item : rows) { for (LbDailyUserTradeReport item : rows) {
Row row = sheet.createRow(r++); Row row = sheet.createRow(r++);
row.createCell(0).setCellValue(item.getNickname() == null ? "" : item.getNickname()); row.setHeightInPoints(row.getHeightInPoints() * rowHeightFactor);
row.createCell(1).setCellValue(asDouble(item.getYestodayBuyAmt())); boolean isSummary = "report_sum".equals(item.getDataType());
row.createCell(2).setCellValue(asDouble(item.getDailySellAmt())); boolean zebra = (r % 2 == 0);
row.createCell(3).setCellValue(asDouble(item.getDailyBuyAmt()));
row.createCell(4).setCellValue(asDouble(item.getServiceAmt())); CellStyle tStyle = isSummary ? summaryTextStyle : (zebra ? zebraTextStyle : textStyle);
row.createCell(5).setCellValue(asDouble(item.getDiffAmt())); CellStyle nStyle = isSummary ? summaryIntStyle : (zebra ? zebraIntStyle : intStyle);
row.createCell(6).setCellValue(asDouble(item.getActualReceiptsPayments()));
row.createCell(7).setCellValue(asDouble(item.getDikouAmt())); createTextCell(row, 0, item.getNickname() == null ? "" : item.getNickname(), tStyle);
createIntCell(row, 1, roundedInt(item.getYestodayBuyAmt()), nStyle);
createIntCell(row, 2, roundedInt(item.getDailySellAmt()), nStyle);
createIntCell(row, 3, roundedInt(item.getDailyBuyAmt()), nStyle);
createIntCell(row, 4, roundedInt(item.getServiceAmt()), nStyle);
createIntCell(row, 5, roundedInt(item.getDiffAmt()), nStyle);
createIntCell(row, 6, roundedInt(item.getActualReceiptsPayments()), nStyle);
createIntCell(row, 7, roundedInt(item.getDikouAmt()), nStyle);
}
if (r > 1) {
SheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule negative = scf.createConditionalFormattingRule(ComparisonOperator.LT, "0");
FontFormatting ff = negative.createFontFormatting();
ff.setFontColorIndex(IndexedColors.DARK_RED.getIndex());
ff.setFontStyle(false, false);
scf.addConditionalFormatting(new CellRangeAddress[]{new CellRangeAddress(1, r - 1, 5, 5)}, negative);
} }
for (int i = 0; i <= 7; i++) { for (int i = 0; i <= 7; i++) {
sheet.autoSizeColumn(i); sheet.autoSizeColumn(i);
int currentWidth = sheet.getColumnWidth(i);
int widened = (int) Math.min(255 * 256, Math.round(currentWidth * 1.5));
sheet.setColumnWidth(i, widened);
} }
String filename = "lbDailyUserTradeReport_" + parsedReportDate + "_" + tenantId.trim() + ".xlsx"; String filename = "当日汇总报表" + parsedReportDate.format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".xlsx";
String encoded = URLEncoder.encode(filename, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); String encoded = URLEncoder.encode(filename, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); response.setCharacterEncoding(StandardCharsets.UTF_8.name());
@@ -466,7 +566,28 @@ public class LbDailyUserTradeReportController {
} }
} }
private static double asDouble(BigDecimal value) { private static long roundedInt(BigDecimal value) {
return value == null ? 0d : value.doubleValue(); if (value == null) {
return 0L;
}
return value.setScale(0, RoundingMode.HALF_UP).longValue();
}
private static void createHeaderCell(Row row, int col, String text, CellStyle style) {
Cell cell = row.createCell(col);
cell.setCellValue(text);
cell.setCellStyle(style);
}
private static void createTextCell(Row row, int col, String text, CellStyle style) {
Cell cell = row.createCell(col);
cell.setCellValue(text);
cell.setCellStyle(style);
}
private static void createIntCell(Row row, int col, long value, CellStyle style) {
Cell cell = row.createCell(col);
cell.setCellValue((double) value);
cell.setCellStyle(style);
} }
} }