xyc
3 小时以前 5b41c8896ef7850be2672e45b69bb58e149729ec
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
@@ -42,6 +42,7 @@
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
@@ -5607,6 +5608,34 @@
        return last == 0.0 ? 0.0 : (cur - last) / last;
    }
    /** Force a data format while preserving all other cell style attributes. */
    static int smSetPercentFormat(Sheet sh, int firstRow0, int lastRow0, int col0, String format) {
        if (sh == null || format == null || format.trim().isEmpty()) return 0;
        DataFormat dataFormat = sh.getWorkbook().createDataFormat();
        short formatIndex = dataFormat.getFormat(format);
        Map<Short, CellStyle> convertedStyles = new HashMap<>();
        int changed = 0;
        for (int r = firstRow0; r <= lastRow0; r++) {
            Row row = sh.getRow(r);
            if (row == null) continue;
            Cell cell = row.getCell(col0);
            if (cell == null) continue;
            CellStyle current = cell.getCellStyle();
            if (current != null && format.equals(current.getDataFormatString())) continue;
            short sourceStyle = current == null ? -1 : current.getIndex();
            CellStyle converted = convertedStyles.get(sourceStyle);
            if (converted == null) {
                converted = sh.getWorkbook().createCellStyle();
                if (current != null) converted.cloneStyleFrom(current);
                converted.setDataFormat(formatIndex);
                convertedStyles.put(sourceStyle, converted);
            }
            cell.setCellStyle(converted);
            changed++;
        }
        return changed;
    }
    /** 写值:公式格不动(保留母版公式,打开即重算);0 且原为空格则不新增 */
    private int smSetVal(Sheet sh, int r0, int c0, double v) {
        Row row = sh.getRow(r0);
@@ -5688,7 +5717,7 @@
    }
    /** 城市分市州页:4 块(累计客运量/累计周转量/当月客运量/当月周转量),每块 全省 1 行 + 17 市州 */
    private int smWriteCityByRegion(XSSFWorkbook wb, int month,
    int smWriteCityByRegion(XSSFWorkbook wb, int month,
                                    Map<String, double[]> curCum, Map<String, double[]> lastCum,
                                    Map<String, double[]> curMon, Map<String, double[]> lastMon) {
        Sheet sh = wb.getSheet("城市分市州");
@@ -5700,8 +5729,8 @@
        int n = 0;
        n += smWriteCityByRegionBlock(sh, 4, true, curCum, lastCum);
        n += smWriteCityByRegionBlock(sh, 26, false, curCum, lastCum);
        n += smWriteCityByRegionBlock(sh, 49, true, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 71, false, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 48, true, curMon, lastMon);
        n += smWriteCityByRegionBlock(sh, 70, false, curMon, lastMon);
        return n;
    }
@@ -5773,6 +5802,9 @@
            n += smSetVal(sh, rows[i] + 9, 5, v[1]);
            n += smSetVal(sh, rows[i] + 9, 6, v[3]);
        }
        // Older templates may retain an integer format on the monthly turnover YoY cells.
        // Enforce the format here so generated workbooks remain correct even when the expansion source is stale.
        smSetPercentFormat(sh, 11, 15, 6, "0.00%");
        return n;
    }