用 StringBuilder 拼接 HTML 表格最稳,需预估容量、分离固定结构、仅最后调用 toString();用户输入须 HTML 转义防 XSS;金额用 DecimalFormat 显式指定 US Locale 格式化;CSV 导出应使用 OpenCSV 或 Commons CSV 库并添加 UTF-8 BOM。
StringBuilder 拼接报表 HTML 表格最稳直接用 + 拼接多行 HTML 报表字符串,在循环中极易触发频繁对象创建,导致 GC 压力大、生成慢。尤其当数据量超 500 行时,String 拼接耗时可能翻倍。
实操建议:
StringBuilder 时预估容量,比如每行约 120 字符 × 1000 行 → 设为 new StringBuilder(120_000)
toString(),只在最后生成完成时调用一次StringBuilder report = new StringBuilder(120_000);
report.append("| ID | 姓名 | 金额 |
|---|---|---|
| ") .append(order.getId()) .append(" | ") .append(escapeHtml(order.getName())) // 防 XSS,见下节 .append(" | ") .append(String.format("%.2f", order.getAmount())) .append(" |
t.toString(); // 仅此处 toString()
报表字段若含 、&、" 等字符(比如姓名是 "张三"),不转义会导致 HTML 结构错乱,甚至执行脚本。
不要自己写正则替换 —— 容易漏掉 ' 或 Unicode 变体。用成熟工具更可靠:
StringEscapeUtils.escapeHtml4()(来自 Apache Commons Text)org.springframework.web.util.HtmlUtils.htmlEscape()
private static String escapeHtml(String s) {
if (s == null) return "";
return s.replace("&", "&")
.replace("<", "zuojiankuohaophpcn")
.replace(">", "youjiankuohaophpcn")
.replace("\"", """);
}
DecimalFormat 格式化金额比 String.format 更可控报表里金额字段要求统一显示为“千分位 + 两位小数”,比如 1234567.89 → 1,234,567.89。用 String.format("%,.2f", d) 看似简单,但受 Locale 影响大:在德国环境会输出 1.234.567,89,和中文报表预期不符。
实操建议:
Locale.US,或直接用 DecimalFormat 控制符号DecimalFormat 实例(线程安全),避免每次 newDouble.NaN 或 Infinity 会抛异常,需提前判空private static final DecimalFormat AMOUNT_FORMAT = new DecimalFormat("#,##0.00");
static {
AMOUNT_FORMAT.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
}
// 使用时:
String amtStr = AMOUNT_FORMAT.format(order.getAmount());
报表常需同时支持 HTML 和 CSV 导出。用 StringBuilder 手拼 CSV 很危险:字段含逗号、换行、双引号时,必须按 RFC 4180 规则加引号并转义,手写极易出错(比如漏转义嵌套双引号 "a""b")。
直接上轻量库更省心:
CsvWriter 支持自动转义,适合单线程导出QuoteMode.ALL_NON_NULL 可控性更强BufferedWriter.write(line + "\n") 这类裸写法 —— 换行符在 Windows/Linux 下不一致,库会自动适配容易被忽略的一点:CSV 文件的 BOM 头。中文 Excel 打开乱码,大概率是因为没加 \uFEFF。用 Commons CSV 时需手动在输出流开头写入:
try (OutputStream os = response.getOutputStream()) {
os.write(0xEF); os.write(0xBB); os.write(0xBF); // UTF-8 BOM
try (CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(os, StandardCharsets.UTF_8),
CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL_NON_NULL))) {
printer.printRecord("ID", "姓名", "金额");
for (Order o : orders) {
printer.printRecord(o.getId(), o.getName(), o.getAmount());
}
}
}