easyexcel 可以做到一个页签用模板填充,一个页直接写入吗?

yshpjwxd  于 4个月前  发布在  其他
关注(0)|答案(1)|浏览(87)

建议先去看文档
快速开始常见问题
异常代码

String templateFileName = "E:\\xnworkspace\\mgmt-data-analysis\\src\\main\\resources\\template\\mainReportTpl.xlsx";
        String fileName = "E:\\xnworkspace\\mgmt-data-analysis\\sdf.xlsx";

        MainReportData data = projectService.mainReportData(41);
        List<DetailReportData> mainReportData = projectService.detailReportData(41);
        DetailReportData.replaceStatusField(mainReportData);
        List<OnceAbsoluteMergeStrategy> mergeStrategyList = DetailReportData.getMergeStrategy(mainReportData);
        List<HorizontalCellStyleStrategy> styleStrategyList = DetailReportData.getStyleStrategy(mainReportData);
        ExcelWriterBuilder write = EasyExcel.write(fileName);
        write.withTemplate(templateFileName).sheet().doFill(data);

        ExcelWriterSheetBuilder sheet2 = write.head(DetailReportData.class).sheet(1, "任务详情");
        for (OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy : mergeStrategyList) {
            sheet2.registerWriteHandler(onceAbsoluteMergeStrategy);
        }
        for (HorizontalCellStyleStrategy horizontalCellStyleStrategy : styleStrategyList) {
            sheet2.registerWriteHandler(horizontalCellStyleStrategy);
        }
        sheet2.doWrite(mainReportData);

异常提示

建议描述
目前的情况是第一个只要加上了第二个页签的处理,第一个页签的数据就是空的

6vl6ewon

6vl6ewon1#

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.style.HorizontalCellStyleStrategy;

import java.util.List;

public class ExcelExport {

    public static void main(String[] args) {
        String templateFileName = "E:\\xnworkspace\\mgmt-data-analysis\\src\\main\\resources\\template\\mainReportTpl.xlsx";
        String fileName = "E:\\xnworkspace\\mgmt-data-analysis\\sdf.xlsx";

        // 获取数据
        MainReportData data = projectService.mainReportData(41);
        List<DetailReportData> mainReportData = projectService.detailReportData(41);

        // 对数据进行预处理
        DetailReportData.replaceStatusField(mainReportData);
        List<OnceAbsoluteMergeStrategy> mergeStrategyList = DetailReportData.getMergeStrategy(mainReportData);
        List<HorizontalCellStyleStrategy> styleStrategyList = DetailReportData.getStyleStrategy(mainReportData);

        // 创建Excel写入器
        ExcelWriterBuilder write = EasyExcel.write(fileName);

        // 使用模板文件填充数据
        write.withTemplate(templateFileName).sheet().doFill(data);

        // 创建第二个sheet并写入数据
        ExcelWriterSheetBuilder sheet2 = write.head(DetailReportData.class).sheet(1, "任务详情");
        for (OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy : mergeStrategyList) {
            sheet2.registerWriteHandler(onceAbsoluteMergeStrategy);
        }
        for (HorizontalCellStyleStrategy horizontalCellStyleStrategy : styleStrategyList) {
            sheet2.registerWriteHandler(horizontalCellStyleStrategy);
        }
        sheet2.doWrite(mainReportData);
    }
}

相关问题