| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Cell; |
| | | import org.apache.poi.ss.usermodel.CellType; |
| | | import org.apache.poi.ss.usermodel.FormulaEvaluator; |
| | | import org.apache.poi.ss.usermodel.Row; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | |
| | | if (sh == null || expanded.contains(sh.getSheetName())) continue; |
| | | shiftQualifiedRefs(sh, insertColOf); |
| | | } |
| | | recalcAndCache(wb); |
| | | int titleFixed = applyTitles(wb, year, targetMonth); |
| | | if (titleFixed > 0) { |
| | | log.info("扩列:标题按 {}年{}月 重写 {} 格", year, targetMonth, titleFixed); |
| | |
| | | /** 只把 7 个标题页的标题按给定期次重写(用于修正母版里滞后的标题)。 */ |
| | | public byte[] fixTitles(byte[] source, int year, int month) { |
| | | try (XSSFWorkbook wb = new XSSFWorkbook(new ByteArrayInputStream(source))) { |
| | | recalcAndCache(wb); |
| | | int n = applyTitles(wb, year, month); |
| | | wb.setForceFormulaRecalculation(true); |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | |
| | | } |
| | | } |
| | | |
| | | /** 整本重算并把结果写入缓存值:否则母版里只有公式没有值,后续读取会把空值当 0(2026-10-12 修) */ |
| | | private void recalcAndCache(XSSFWorkbook wb) { |
| | | try { |
| | | FormulaEvaluator ev = wb.getCreationHelper().createFormulaEvaluator(); |
| | | ev.clearAllCachedResultValues(); |
| | | ev.evaluateAll(); |
| | | } catch (Exception e) { |
| | | log.warn("扩列:整本重算未完成(公式仍在,Excel 打开会再算):{}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** 把 7 个页签的标题文字按「累计=1-M月、当月=M月」重写;命中标记才改,避免误伤备注。 */ |
| | | private int applyTitles(XSSFWorkbook wb, int year, int month) { |
| | | int changed = 0; |