Use the easyexcel framework to write custom formats to excel in java

    General excel examples are to write list data to excel, with a header in the format, and the content is filled using a List collection. The demand I encountered today is a bit different. It also needs to be written to excel, but the content at the beginning is not a list, but a part similar to a summary, followed by a table. The general structure is as follows:

    

 

    For this requirement, there is no way to use common excel operations. The solution is to use a template to populate it. We need to create an excel template file and use placeholders for the changed parts.

    As shown below, we create a template file:

    In the template, the front part can be filled with map data, so the placeholder is {checkTime}. The following part of the loop list is filled with {.name}, {.result}, {.desc}.

    Above code:

    pom.xml

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel-core</artifactId>
	<version>3.2.1</version>
</dependency>

    List entity class:


import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class CheckRecord {
    private String name;
    private String result;
    private String desc;
}

    Test class:

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class RecordExportTest {

    private static final String RECORD_TEMPLATE = "conf/record-template.xlsx";

    public static void main(String[] args) {
        String filePath = "e:\\record-" + System.currentTimeMillis() + ".xlsx";
        Map<String, Object> summary = new HashMap<>();
        summary.put("checkTime", "2023-04-18");
        summary.put("checkResult", "success");
        summary.put("versionInfo", "v1.0.0");
        List<CheckRecord> list = new ArrayList<>();
        list.add(new CheckRecord("disk status", "success", "磁盘状态"));
        list.add(new CheckRecord("network status", "success", "网络状态"));
        boolean isOk = saveData(filePath, summary, list);
        System.out.println("write excel : " + isOk);
    }

    private static boolean saveData(String filePath, Map<String, Object> summary, List<CheckRecord> list) {
        try (ExcelWriter excelWriter = EasyExcel.write(filePath).withTemplate(RECORD_TEMPLATE).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
            excelWriter.fill(list, fillConfig, writeSheet);
            excelWriter.fill(summary, writeSheet);
            excelWriter.finish();
        }
        return true;
    }
}

    Put the record-template.xlsx file prepared earlier into the project conf directory according to the code requirements, and then run the example. The generated excel file is as follows:

 

 

    With such template filling, our excel can be made more complex. 

Guess you like

Origin blog.csdn.net/feinifi/article/details/130231051