In Java development, building complex multi-level headers is a common and challenging task when working with Excel files. This article will introduce in detail how to easily implement multi-level header building and write data to Excel files through the customized HeaderBuilder class combined with the EasyExcel library.
1. Overall idea
Our goal is to create a flexible and scalable way to build multi-level headers. To do this, we designed a HeaderBuilder class that uses a tree structure to represent header information. Each header node (HeaderNode) can have multiple child nodes, forming a multi-level structure. Finally, by traversing this tree structure, header data in the form of nested lists suitable for EasyExcel is generated.
2. Key code implementation
HeaderNode class
package ; import ; import ; import ; @Data public class HeaderNode { private String title; private List<HeaderNode> children; public HeaderNode(String title) { = title; = new LinkedList<>(); } public HeaderNode addChild(HeaderNode child) { (child); return this; } }
passaddChild
Methods: You can easily add child nodes to the current node and build a tree structure.
HeaderBuilder class
package ; import .*; public class HeaderBuilder { private final HeaderNode root; private HeaderBuilder(HeaderNode root) { = root; } public static HeaderBuilder create(HeaderNode... headerNodes) { HeaderNode root = new HeaderNode("ROOT"); // Virtual root node for (HeaderNode node : headerNodes) { (node); } return new HeaderBuilder(root); } /** * Generate multi-level header structure * * @return Nested header structure for EasyExcel */ public List<List<String>> build() { List<List<String>> result = new ArrayList<>(); traverse(new LinkedList<>(), root, result); return result; } private void traverse(LinkedList<String> currentPath, HeaderNode node, List<List<String>> result) { if (() == null || ().isEmpty()) { (new ArrayList<>(currentPath)); return; } for (HeaderNode child : ()) { (()); traverse(currentPath, child, result); (); } } }
The HeaderBuilder class is responsible for managing and generating header structures. It contains a private root property that points to the root node of the header tree structure. The create method is a static factory method used to create a HeaderBuilder instance. It accepts multiple HeaderNodes as parameters and adds these nodes under the virtual ROOT root node.
The build method is the core method, which performs deep-first traversal of the table header tree structure by calling the private traverse method. During the traversal process, the traverse method collects the node name on the current path into the currentPath. When a leaf node (node without child nodes) is encountered, the content of currentPath is added to the result list result, and the header data in the form of nested lists suitable for EasyExcel is finally generated.
Main program class
package ; import ; import ; import ; import ; import ; import ; public class Main { public static void main(String[] args) { // Build the header structure HeaderNode user = new HeaderNode("user") .addChild(new HeaderNode("Name")) .addChild(new HeaderNode("age")) .addChild(new HeaderNode("gender")); HeaderNode address = new HeaderNode("address") .addChild(new HeaderNode("province")) .addChild(new HeaderNode("City")); HeaderNode score = new HeaderNode("Fraction"); List<List<String>> header = (user, address, score).build(); (header); // Prepare data List<List<String>> data = new ArrayList<>(); (("Zhang San", "25", "male", "Zhejiang", "Hangzhou", "20")); (("Li Si", "30", "female", "Jiangsu", "Nanjing", "20")); ExcelWriter excelWriter = ("").build(); WriteSheet sheet = () .head(header) .build(); (data, sheet); (); } }
In the Main class, the tree structure of the table header is first constructed. By creating a HeaderNode instance and calling the addChild method, a multi-level header node related to "user", "address" and "score" is built. Then, use the method to create the HeaderBuilder instance and call the build method to generate the header data in the form of a nested list.
Then, prepare the data to be written to Excel. Finally, use the EasyExcel library to create ExcelWriter and WriteSheet instances, writing the generated table headers and data to an Excel file named.
This is the end of this article about Java combining EasyExcel to build complex multi-level table headers. For more related Java EasyExcel multi-level table headers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!