Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

动态生成复杂表头,求助 #3824

Open
codenergy opened this issue Jun 6, 2024 · 1 comment
Open

动态生成复杂表头,求助 #3824

codenergy opened this issue Jun 6, 2024 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@codenergy
Copy link

问题描述

Snipaste_2024-06-06_17-31-08

大佬们,遇到一个对我来说比较复杂的导出需求,导出格式表格形式如上。其中列名中的站点1/站点2/站点3都是根据数据库里实际数据动态扩展的。每个站点都有两个指标的数据,因此站点所在的单元格是合并的,求助各位大佬,这种样式,使用EasyExcel要怎么实现呢?

            excelWriter.fill(new FillWrapper("stationStatus", stationNames), fillConfig, writeSheet);// 填充表头站点
            excelWriter.fill(new FillWrapper("stationStatus", indexData), fillConfig, writeSheet); // 填充表头“单日发电量(kWh)”和“总发电量(kWh)”
            excelWriter.fill(new FillWrapper("stationStatus", stationStatusData), writeSheet); // 填充全部的数据

我之前想上面代码一样实现,发现站点下的那一行表头“单日发电量(kWh)”和“总发电量(kWh)”填充不对,会被覆盖
如下图:

image

@codenergy codenergy added the help wanted Extra attention is needed label Jun 6, 2024
@chuhuilove
Copy link

这个需求,有个思路,动态生成表头,数据用List<Map<Integer,Object>>去填充,head中headMap的索引值和数据中的Map<Integer,Object>能一对一。简单写了一段示例,可以参考一下,样式自己调。

 
    @Test
    public void issue3824Test() {

//        https://github.com/alibaba/easyexcel/issues/3824

        // 数据平铺,动态表头

        String fileName = TestFileUtil.getPath() + "issue-3824-" + System.currentTimeMillis() + ".xlsx";

        EasyExcel.write(fileName)
                .head(issue3824Heads())
                .sheet("first")
                .doWrite(issue3824Data());

    }


    public static List<List<String>> issue3824Heads() {

        List<List<String>> heads = Lists.newArrayList();
        heads.add(Lists.newArrayList("日期")); // index 0
        heads.add(Lists.newArrayList("单日发电总量(kWh)")); // index 1
        heads.add(Lists.newArrayList("累计发电总量(kWh)")); // index 2

        // 生成3个站点
        
        for (int i = 0; i < 3; i++) {
            String title = "站点" + (i + 1);

            List<String> head1 = Lists.newArrayList(title);
            head1.add("单日发电总量(kWh)"); // index 3,5,7

            List<String> head2 = Lists.newArrayList(title);

            head2.add("总发电量(kWh)");  // index 4,6,8

            heads.add(head1);
            heads.add(head2);
        }
        return heads;
    }

    public static List<Map<Integer, Object>> issue3824Data() {


        Map<Integer, Object> data1 = new HashMap<>();
        Map<Integer, Object> data2 = new HashMap<>();
        Map<Integer, Object> data3 = new HashMap<>();

        List<Map<Integer, Object>> data = Lists.newArrayList(data1, data2, data3);

        // 数据的索引值和head的索引值,需要一对一
        data1.put(0, "2024-05-06");
        data1.put(1, 10023);
        data1.put(2, 2003);
        data1.put(3, 1002);
        data1.put(4, 3003);
        data1.put(5, 1002);
        data1.put(6, 3003);
        data1.put(7, 1002);
        data1.put(8, 3003);


        data2.put(0, "2024-05-07");
        data2.put(1, 10023);
        data2.put(2, 2003);
        data2.put(3, 4563);
        data2.put(4, 7896);
        // 不存在的值,用空字符串填充
        // 可能有bug
        data2.put(5, "");
        data2.put(6, "");
        data2.put(7, 455);
        data2.put(8, 102);


        data3.put(0, "2024-05-08");
        data3.put(1, 10023);
        data3.put(2, 2003);

        data3.put(5, 4563);
        data3.put(6, 7896);
        data3.put(7, 455);
        data3.put(8, 102);


        return data;
    }
   

Screenshot 2024-06-23 at 17 29 29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants