package com.bonus.common.biz.config;

import com.bonus.common.biz.utils.NumberToChinese;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 导出模版样式
 * @author ma_sh
 */
public class PoiOutPage {

    /**
     * 生成带有样式的 Excel 文件
     * @param result
     * @param list
     * @param filename
     * @return
     */
    public static HSSFWorkbook excel(List<Map<String, Object>> result, List<String> list, String filename) {
        // 获取工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyle(workbook);

        // 设置默认列宽
        sheet.setDefaultColumnWidth(15);

        // 设置工作簿名称
        workbook.setSheetName(0, filename);

        // 如果数据为空，创建一个空的工作表
        if (result == null || result.size() == 0) {
            createEmptySheet(sheet, list, titleStyle, headerStyle, filename);
        } else {
            // 如果数据不为空，创建工作表并填充数据
            int nColumn = list.size();
            int rowNum = createTitleRow(sheet, titleStyle, filename, nColumn);
            rowNum = createHeaderRow(sheet, headerStyle, list, rowNum);
            createDataRows(sheet, result, contentStyle, nColumn, rowNum);
        }

        return workbook;
    }

    /**
     * 创建标题行
     * @param sheet
     * @param titleStyle
     * @param filename
     * @param nColumn
     * @return
     */
    private static int createTitleRow(HSSFSheet sheet, HSSFCellStyle titleStyle, String filename, int nColumn) {
        HSSFRow row = sheet.createRow(0);
        row.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (short) (nColumn - 1)));
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue(filename);
        return 1; // 下一行是表头
    }

    /**
     * 创建表头行
     * @param sheet
     * @param headerStyle
     * @param list
     * @param rowNum
     * @return
     */
    private static int createHeaderRow(HSSFSheet sheet, HSSFCellStyle headerStyle, List<String> list, int rowNum) {
        HSSFRow row = sheet.createRow(rowNum);
        row.setHeightInPoints(20);
        for (int j = 0; j < list.size(); j++) {
            HSSFCell cell = row.createCell(j);
            String header = list.get(j);
            cell.setCellStyle(headerStyle);
            cell.setCellValue(header != null ? header : "");
        }
        return rowNum + 1;
    }

    /**
     * 填充数据行
     * @param sheet
     * @param result
     * @param contentStyle
     * @param nColumn
     * @param rowNum
     */
    private static void createDataRows(HSSFSheet sheet, List<Map<String, Object>> result, HSSFCellStyle contentStyle, int nColumn, int rowNum) {
        for (Map<String, Object> resultRow : result) {
            List<Object> rowData = map2List(resultRow);
            HSSFRow row = sheet.createRow(rowNum++);
            row.setHeightInPoints(15);

            // 第一列：单位数据
            if (rowData.size() != nColumn) {
                HSSFCell cell = row.createCell(0);
                cell.setCellStyle(contentStyle);
                cell.setCellValue(rowData.get(0).toString());

                sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 1, (short) (nColumn - 1)));
                continue;
            }

            // 填充数据行
            for (int j = 0; j < nColumn; j++) {
                HSSFCell cell = row.createCell(j);
                Object data = rowData.get(j);
                if (data != null) {
                    setCellValue(cell, contentStyle, data);
                } else {
                    cell.setCellStyle(contentStyle);
                    cell.setCellValue("");
                }
            }
        }
    }

    /**
     * 设置单元格数据
     * @param cell
     * @param contentStyle
     * @param data
     */
    private static void setCellValue(HSSFCell cell, HSSFCellStyle contentStyle, Object data) {
        if (isNumeric(data)) {
            contentStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));
            cell.setCellStyle(contentStyle);
            cell.setCellValue(Double.parseDouble(data.toString()));
        } else {
            cell.setCellStyle(contentStyle);
            cell.setCellValue(data.toString());
        }
    }

    /**
     * 创建一个空的工作表
     * @param sheet
     * @param list
     * @param titleStyle
     * @param headerStyle
     * @param filename
     */
    private static void createEmptySheet(HSSFSheet sheet, List<String> list, HSSFCellStyle titleStyle, HSSFCellStyle headerStyle, String filename) {
        int nColumn = list.size();
        int rowNum = createTitleRow(sheet, titleStyle, filename, nColumn);
        createHeaderRow(sheet, headerStyle, list, rowNum);
    }


    /**
     * 生成带有样式的 Excel 文件
     *
     * @param result       数据内容（每行是一个 Map）
     * @param list         表头
     * @param filename     文件名
     * @param unit         单位信息
     * @param projectName  项目名称
     * @return HSSFWorkbook 生成的 Excel 文件
     */
    public static HSSFWorkbook excelForcheck(List<Map<String, Object>> result, List<String> list,
                                             String filename, String unit, String projectName) {
        // 创建工作簿和工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(15); // 设置列宽

        // 创建样式
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyle(workbook);

        // 设置工作簿名称
        workbook.setSheetName(0, filename);

        // 填充标题行
        int rowNum = 0;
        rowNum = createTitleRow(sheet, rowNum, filename, titleStyle, list.size());
        rowNum = createProjectInfoRow(sheet, rowNum, projectName, unit, titleStyle, list.size());

        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, list, headerStyle);

        // 填充数据行
        if (result != null && !result.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, result, contentStyle, list.size());
        } else {
            // 如果没有数据，则仅显示表头
            rowNum++;
        }

        return workbook;
    }

    public static HSSFWorkbook excelForcheckTwo(List<Map<String, Object>> result, List<String> list, String filename, String projectName, String unit, BigDecimal totalCost) {
        // 创建工作簿和工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(15); // 设置列宽

        // 创建样式
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyleCost(workbook);

        // 设置工作簿名称
        workbook.setSheetName(0, filename);

        // 填充标题行
        int rowNum = 0;
        rowNum = createTitleRowStyle(sheet, rowNum, filename, titleStyle, list.size());
        rowNum = createProjectAndUnitRow(sheet, rowNum, projectName, unit, titleStyle, list.size());

        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, list, headerStyle);

        // 填充数据行
        if (result != null && !result.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, result, contentStyle, list.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, result, contentStyle, list.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (list.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (list.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, list, totalCost, headerStyle);
        return workbook;
    }

    public static HSSFWorkbook excelForcheckAll(List<Map<String, Object>> resultLease,List<Map<String, Object>> resultLose,List<Map<String, Object>> resultRepair,List<Map<String, Object>> resultScrap,List<Map<String, Object>> resultsReduction,
                                                List<String> listLease,List<String> listLose,List<String> listRepair,List<String> listScrap,List<String> listReduction, String filename, String projectName, String unit,
                                                BigDecimal totalCostLease, BigDecimal totalCostLose, BigDecimal totalCostRepair, BigDecimal totalCostScrap,BigDecimal totalCostReduction) {
        // 创建工作簿和工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(15); // 设置列宽

        // 创建样式
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyleCost(workbook);

        // 设置工作簿名称
        workbook.setSheetName(0, filename);

        // 填充标题行
        int rowNum = 0;
        rowNum = createTitleRowStyle(sheet, rowNum, filename, titleStyle, listLease.size());
        rowNum = createProjectAndUnitRow(sheet, rowNum, projectName, unit, titleStyle, listLease.size());

        /**
         * 租赁费用明细
         */
        rowNum = createTitleRowStyleCost(sheet, rowNum, "租赁费用明细", titleStyle, listLease.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLease, headerStyle);

        // 填充数据行
        if (resultLease != null && !resultLease.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLease, totalCostLease, headerStyle);

        /**
         * 维修费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "维修费用明细", titleStyle, listRepair.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listRepair, headerStyle);
        // 填充数据行
        if (resultRepair!= null &&!resultRepair.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listRepair, totalCostRepair, headerStyle);

        /**
         * 报废费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "报废费用明细", titleStyle, listScrap.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listScrap, headerStyle);
        // 填充数据行
        if (resultScrap!= null &&!resultScrap.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listScrap, totalCostScrap, headerStyle);

        /**
         * 丢失费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "丢失费用明细", titleStyle, listLose.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLose, headerStyle);
        // 填充数据行
        if (resultLose!= null &&!resultLose.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLose, totalCostLose, headerStyle);

        /**
         * 减免费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "减免费用明细", titleStyle, listReduction.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listReduction, headerStyle);
        // 填充数据行
        if (resultsReduction != null && !resultsReduction.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultsReduction, contentStyle, listReduction.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listReduction, totalCostReduction, headerStyle);


        //合计
        BigDecimal totalCostAll = totalCostLease.add(totalCostLose).add(totalCostRepair).add(totalCostScrap).add(totalCostReduction);
        rowNum = createTotalRowAll(sheet, rowNum, listScrap, totalCostAll, headerStyle);

        return workbook;
    }

    public static void excelForcheckEach(HSSFWorkbook workbook,HSSFSheet sheet,List<Map<String, Object>> resultLease,List<Map<String, Object>> resultLose,List<Map<String, Object>> resultRepair,List<Map<String, Object>> resultScrap,List<Map<String, Object>> resultsReduction,
                                                List<String> listLease,List<String> listLose,List<String> listRepair,List<String> listScrap,List<String> listReduction, String projectName, String unit,
                                                BigDecimal totalCostLease, BigDecimal totalCostLose, BigDecimal totalCostRepair, BigDecimal totalCostScrap,BigDecimal totalCostReduction) {

        sheet.setDefaultColumnWidth(15); // 设置列宽

        // 创建样式
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyleCost(workbook);

//        // 设置工作簿名称
//        workbook.setSheetName(0, sheet.getSheetName());

        // 填充标题行
        int rowNum = 0;
        rowNum = createTitleRowStyle(sheet, rowNum, "结算费用明细", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRow(sheet, rowNum, projectName, unit, titleStyle, listLease.size());

        /**
         * 租赁费用明细
         */
        rowNum = createTitleRowStyleCost(sheet, rowNum, "租赁费用明细", titleStyle, listLease.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLease, headerStyle);

        // 填充数据行
        if (resultLease != null && !resultLease.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLease, totalCostLease, headerStyle);

        /**
         * 维修费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "维修费用明细", titleStyle, listRepair.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listRepair, headerStyle);
        // 填充数据行
        if (resultRepair!= null &&!resultRepair.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listRepair, totalCostRepair, headerStyle);

        /**
         * 报废费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "报废费用明细", titleStyle, listScrap.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listScrap, headerStyle);
        // 填充数据行
        if (resultScrap!= null &&!resultScrap.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listScrap, totalCostScrap, headerStyle);

        /**
         * 丢失费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "丢失费用明细", titleStyle, listLose.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLose, headerStyle);
        // 填充数据行
        if (resultLose!= null &&!resultLose.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLose, totalCostLose, headerStyle);

        /**
         * 减免费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "减免费用明细", titleStyle, listReduction.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listReduction, headerStyle);
        // 填充数据行
        if (resultsReduction != null && !resultsReduction.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultsReduction, contentStyle, listReduction.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listReduction, totalCostReduction, headerStyle);


        //合计
        BigDecimal totalCostAll = totalCostLease.add(totalCostLose).add(totalCostRepair).add(totalCostScrap).add(totalCostReduction);
        rowNum = createTotalRowAll(sheet, rowNum, listScrap, totalCostAll, headerStyle);

    }


    /**
     * 结算记录查询--导出全部明细
     */
    public static HSSFWorkbook excelForcheckAlls(List<Map<String, Object>> resultLease,List<Map<String, Object>> resultLose,List<Map<String, Object>> resultRepair,List<Map<String, Object>> resultScrap,List<Map<String, Object>> resultsReduction,
                                                List<String> listLease,List<String> listLose,List<String> listRepair,List<String> listScrap,List<String> listReduction, String filename, String projectName, String unit,
                                                BigDecimal totalCostLease, BigDecimal totalCostLose, BigDecimal totalCostRepair, BigDecimal totalCostScrap,BigDecimal totalCostReduction) {
        // 创建工作簿和工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        sheet.setDefaultColumnWidth(15); // 设置列宽

        // 创建样式
        HSSFCellStyle titleStyle = createTitleStyle(workbook);
        HSSFCellStyle headerStyle = createHeaderStyle(workbook);
        HSSFCellStyle contentStyle = createCellStyleCost(workbook);

        // 设置工作簿名称
        workbook.setSheetName(0, filename);

        // 填充标题行
        int rowNum = 0;
        rowNum = createTitleRowStyle(sheet, rowNum, filename, titleStyle, listLease.size());
        rowNum = createTitleRowStyleCost(sheet, rowNum, "本工程清算项目及金额（元）", titleStyle, listLease.size());
        BigDecimal totalCost1 = totalCostLease.subtract(totalCostReduction);
        //合计
        BigDecimal totalCostAll = totalCostLease.add(totalCostLose).add(totalCostRepair).add(totalCostScrap).subtract(totalCostReduction);
        String all= NumberToChinese.number2CN(totalCostAll.setScale(2, RoundingMode.HALF_UP));
        rowNum = createProjectAndUnitRows(sheet, rowNum, "一、施工机具有偿使用费：", totalCost1.setScale(2, RoundingMode.HALF_UP)+"", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRows(sheet, rowNum, "二、施工机具维修费：", totalCostRepair.setScale(2, RoundingMode.HALF_UP)+"", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRows(sheet, rowNum, "三、施工机具丢失费：", totalCostLose.setScale(2, RoundingMode.HALF_UP)+"", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRows(sheet, rowNum, "四、施工机具损坏赔偿费：", totalCostScrap.setScale(2, RoundingMode.HALF_UP)+"", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRowss(sheet,rowNum,"费用合计金额（大写）",all,totalCostAll.setScale(2, RoundingMode.HALF_UP)+"", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRows(sheet, rowNum, "说明", "本协议一式三份，甲方一份，乙方一份，经双方签字后生效。", titleStyle, listLease.size());
        rowNum = createProjectAndUnitRows(sheet, rowNum, "备注", "此费用仅为在机具设备分公司发生费用，未计从项目部领用机具费用。", titleStyle, listLease.size());

        /**
         * 租赁费用明细
         */
        rowNum = createTitleRowStyleCost(sheet, rowNum, "租赁费用明细", titleStyle, listLease.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLease, headerStyle);

        // 填充数据行
        if (resultLease != null && !resultLease.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLease.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLease, totalCostLease, headerStyle);

        /**
         * 维修费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "维修费用明细", titleStyle, listRepair.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listRepair, headerStyle);
        // 填充数据行
        if (resultRepair!= null &&!resultRepair.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultRepair, contentStyle, listRepair.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listRepair.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listRepair, totalCostRepair, headerStyle);

        /**
         * 报废费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "报废费用明细", titleStyle, listScrap.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listScrap, headerStyle);
        // 填充数据行
        if (resultScrap!= null &&!resultScrap.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultScrap, contentStyle, listScrap.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listScrap.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listScrap, totalCostScrap, headerStyle);

        /**
         * 丢失费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "丢失费用明细", titleStyle, listLose.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listLose, headerStyle);
        // 填充数据行
        if (resultLose!= null &&!resultLose.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLose, contentStyle, listLose.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listLose.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listLose, totalCostLose, headerStyle);

        /**
         * 减免费用明细
         */
        // 填充标题行
        rowNum = createTitleRowStyleCost(sheet, rowNum, "减免费用明细", titleStyle, listReduction.size());
        // 填充表头
        rowNum = createHeaderRow(sheet, rowNum, listReduction, headerStyle);
        // 填充数据行
        if (resultsReduction != null && !resultsReduction.isEmpty()) {
            rowNum = createDataRows(sheet, rowNum, resultsReduction, contentStyle, listReduction.size());
        } else {
            // 如果没有数据，则仅显示表头
//            rowNum++;
//            rowNum = createDataRows(sheet, rowNum, resultLease, contentStyle, listLease.size());
            HSSFRow row = sheet.createRow(rowNum++);
            HSSFCell cell = row.createCell(0);
            cell.setCellStyle(headerStyle);
            cell.setCellValue("暂无数据");
            sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1)));
            CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (listReduction.size() - 1));
            // 设置边框样式
            RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
            RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        }
        rowNum = createTotalRow(sheet, rowNum, listReduction, totalCostReduction, headerStyle);

        rowNum = createTotalRowAll(sheet, rowNum, listScrap, totalCostAll, headerStyle);

        return workbook;
    }


    /**
     * 在数据的最后一行添加费用小计
     *
     * @param sheet       工作表
     * @param rowNum      当前行号
     * @param list        列名列表
     * @param totalCost   总费用
     * @param headerStyle 表头样式
     */
    private static int createTotalRow(HSSFSheet sheet, int rowNum, List<String> list, BigDecimal totalCost, HSSFCellStyle headerStyle) {
        HSSFRow row = sheet.createRow(rowNum++);
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(headerStyle);
        cell.setCellValue("费用小计：");

        sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum-1, 1, (short) (list.size() - 2)));
        CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 1, (short) (list.size() - 2));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        // 假设总费用应该放在最后一列
        int totalCostColumnIndex = list.size() - 1;
        cell = row.createCell(totalCostColumnIndex);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(String.format("%.2f", totalCost));

        return rowNum;
    }
    /**
     * 在数据的最后一行添加合计费用
     *
     * @param sheet       工作表
     * @param rowNum      当前行号
     * @param list        列名列表
     * @param totalCost   总费用
     * @param headerStyle 表头样式
     */
    private static int createTotalRowAll(HSSFSheet sheet, int rowNum, List<String> list, BigDecimal totalCost, HSSFCellStyle headerStyle) {
        HSSFRow row = sheet.createRow(rowNum++);
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(headerStyle);
        cell.setCellValue("合计：");

        sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum-1, 1, (short) (list.size() - 2)));
        CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 1, (short) (list.size() - 2));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);
        // 假设总费用应该放在最后一列
        int totalCostColumnIndex = list.size() - 1;
        cell = row.createCell(totalCostColumnIndex);
        cell.setCellStyle(headerStyle);
        cell.setCellValue(String.format("%.2f", totalCost));

        return rowNum;
    }

    /**
     * 创建标题行
     */
    private static int createTitleRow(HSSFSheet sheet, int rowNum, String filename, HSSFCellStyle titleStyle, int nColumn) {
        HSSFRow row = sheet.createRow(rowNum++);
        row.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (short) (nColumn - 1)));
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue(filename);
        return rowNum;
    }

    /**
     * 创建标题行以及样式
     */
    private static int createTitleRowStyle(HSSFSheet sheet, int rowNum, String filename, HSSFCellStyle titleStyle, int nColumn) {
        HSSFRow row = sheet.createRow(rowNum++);
        row.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (short) (nColumn - 1)));
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue(filename);
        // 添加边框
        CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, rowNum - 1, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);

        return rowNum;
    }

    /**
     * 创建标题行以及样式--all
     */
    private static int createTitleRowStyleCost(HSSFSheet sheet, int rowNum, String filename, HSSFCellStyle titleStyle, int nColumn) {
        HSSFRow row = sheet.createRow(rowNum++);
        row.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(rowNum-1, rowNum-1, 0, (short) (nColumn - 1)));
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue(filename);
        // 添加边框
        CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);

        return rowNum;
    }

    /**
     * 创建结算单位和结算工程行
     */
    private static int createProjectAndUnitRow(HSSFSheet sheet, int rowNum, String projectName, String unitName,HSSFCellStyle titleStyle, int nColumn) {
        // 第一行：结算单位
        HSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeightInPoints(30);
        // bug修复：修改合并单元格区域，确保包含两个或以上单元格
        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, 1)); // 结算单位： 占8
        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(titleStyle);
        cell1.setCellValue("结算单位：");

        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 2, (short) (nColumn - 1))); // unitName 占剩余的22
        HSSFCell cell2 = row1.createCell(2);
        cell2.setCellStyle(titleStyle);
        cell2.setCellValue(unitName);

        // 第二行：结算工程
        HSSFRow row2 = sheet.createRow(rowNum++);
        row2.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, 1)); // 结算工程： 占8
        HSSFCell cell3 = row2.createCell(0);
        cell3.setCellStyle(titleStyle);
        cell3.setCellValue("结算工程：");

        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 2, (short) (nColumn - 1))); // projectName 占剩余的22
        HSSFCell cell4 = row2.createCell(2);
        cell4.setCellStyle(titleStyle);
        cell4.setCellValue(projectName);


        // 添加边框
        CellRangeAddress cellRange1 = new CellRangeAddress(rowNum - 2, rowNum - 2, 0, 1);
        CellRangeAddress cellRange2 = new CellRangeAddress(rowNum - 2, rowNum - 2, 2, (short) (nColumn - 1));
        CellRangeAddress cellRange3 = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, 1);
        CellRangeAddress cellRange4 = new CellRangeAddress(rowNum - 1, rowNum - 1, 2, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange4, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange4, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange4, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange4, sheet);


        return rowNum;
    }

    /**
     * 创建结算单位和结算工程行
     */
    private static int createProjectAndUnitRows(HSSFSheet sheet, int rowNum, String projectName, String unitName,HSSFCellStyle titleStyle, int nColumn) {
        // 第一行：结算单位
        HSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeightInPoints(30);
        // bug修复：修改合并单元格区域，确保包含两个或以上单元格
        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, 2)); // 结算单位： 占8
        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(titleStyle);
        cell1.setCellValue(projectName);

        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 3, (short) (nColumn - 1))); // unitName 占剩余的22
        HSSFCell cell2 = row1.createCell(3);
        cell2.setCellStyle(titleStyle);
        cell2.setCellValue(unitName);

        // 添加边框
        CellRangeAddress cellRange1 = new CellRangeAddress(rowNum - 2, rowNum - 2, 0, 2);
        CellRangeAddress cellRange2 = new CellRangeAddress(rowNum - 2, rowNum - 2, 3, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange2, sheet);
        return rowNum;
    }

    private static int createProjectAndUnitRowss(HSSFSheet sheet, int rowNum, String projectName, String unitName,String data,HSSFCellStyle titleStyle, int nColumn) {
        // 第一行：结算单位
        HSSFRow row1 = sheet.createRow(rowNum++);
        row1.setHeightInPoints(30);
        // bug修复：修改合并单元格区域，确保包含两个或以上单元格
        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, 2)); // 结算单位： 占8
        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(titleStyle);
        cell1.setCellValue(projectName);

        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 3, 5)); // 结算单位： 占8
        HSSFCell cell2 = row1.createCell(3);
        cell2.setCellStyle(titleStyle);
        cell2.setCellValue(unitName);

        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 6, (short) (nColumn - 1))); // unitName 占剩余的22
        HSSFCell cell3 = row1.createCell(6);
        cell3.setCellStyle(titleStyle);
        cell3.setCellValue(data);

        // 添加边框
        CellRangeAddress cellRange1 = new CellRangeAddress(rowNum - 2, rowNum - 2, 0, 2);
        CellRangeAddress cellRange2 = new CellRangeAddress(rowNum - 2, rowNum - 2, 3, 5);
        CellRangeAddress cellRange3 = new CellRangeAddress(rowNum - 2, rowNum - 2, 6, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange1, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange2, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange3, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange3, sheet);
        return rowNum;
    }

    /**
     * 创建项目名称和单位信息行
     */
    private static int createProjectInfoRow(HSSFSheet sheet, int rowNum, String projectName, String unit,
                                            HSSFCellStyle titleStyle, int nColumn) {
        HSSFRow row = sheet.createRow(rowNum++);
        row.setHeightInPoints(30);
        sheet.addMergedRegion(new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (nColumn - 1)));
        HSSFCell cell = row.createCell(0);
        cell.setCellStyle(titleStyle);
        cell.setCellValue(projectName + " " + unit);

        // 添加边框
        CellRangeAddress cellRange = new CellRangeAddress(rowNum - 1, rowNum - 1, 0, (short) (nColumn - 1));
        // 设置边框样式
        RegionUtil.setBorderTop(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cellRange, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cellRange, sheet);

        return rowNum;
    }

    /**
     * 创建表头行
     */
    private static int createHeaderRow(HSSFSheet sheet, int rowNum, List<String> list, HSSFCellStyle headerStyle) {
        HSSFRow row = sheet.createRow(rowNum++);
        row.setHeightInPoints(20);
        for (int j = 0; j < list.size(); j++) {
            HSSFCell cell = row.createCell(j);
            String header = list.get(j);
            cell.setCellStyle(headerStyle);
            cell.setCellValue(header != null ? header : "");
        }
        return rowNum;
    }

    /**
     * 创建数据行
     */
    private static int createDataRows(HSSFSheet sheet, int rowNum, List<Map<String, Object>> result,
                                      HSSFCellStyle contentStyle, int nColumn) {
        for (Map<String, Object> resultRow : result) {
            HSSFRow row = sheet.createRow(rowNum++);
            row.setHeightInPoints(15);
            List<Object> rowData = map2List(resultRow);

            for (int j = 0; j < nColumn; j++) {
                HSSFCell cell = row.createCell(j);
                Object data = rowData.get(j);
                setCellData(cell, data, contentStyle);
            }
        }
        return rowNum;
    }

    /**
     * 设置单元格数据，并自动判断数据类型
     */
    private static void setCellData(HSSFCell cell, Object data, HSSFCellStyle contentStyle) {
        if (data != null) {
            if (isNumeric(data)) {
                cell.setCellStyle(contentStyle);
                cell.setCellValue(Double.parseDouble(data.toString()));
            } else {
                cell.setCellStyle(contentStyle);
                cell.setCellValue(data.toString());
            }
        } else {
            cell.setCellStyle(contentStyle);
            cell.setCellValue("");
        }
    }

    /**
     * 判断是否是数字
     */
    private static boolean isNumeric(Object data) {
        if (data == null) {
            return false;
        }
        return data.toString().matches("^(-?\\d+)(\\.\\d+)?$");
    }

    /**
     * 将 Map 转换为 List
     */
    private static List<Object> map2List(Map<String, Object> map) {
        List<Object> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            list.add(entry.getValue());
        }
        return list;
    }


    /**
     * 创建标题样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createTitleStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 14);
        style.setFont(font);
        return style;
    }

    /**
     * 创建表头样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createHeaderStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        // 设置边框
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        // 设置边框颜色为黑色
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());

        return style;
    }

    /**
     * 创建内容样式
     * @param workbook
     * @return
     */
    private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook) {

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        // 设置边框
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        // 设置边框颜色为黑色
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());

        return style;
    }

    /**
     * 创建内容样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createCellStyleCost(HSSFWorkbook workbook) {

        HSSFCellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        HSSFFont font = workbook.createFont();
        font.setFontHeightInPoints((short) 10);
        style.setFont(font);

        // 设置边框
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        // 设置边框颜色为黑色
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());


        // 设置数字格式为保留两位小数
        DataFormat dataFormat = workbook.createDataFormat();
        style.setDataFormat(dataFormat.getFormat("0.00"));  // 设置格式为"0.00"

        return style;
    }
}
