When we do statistics, we need to export some excel in a specific format. We can use POI to write code to deal with formatting, which will result in a lot of code to fix. If we make a small change to the position or color marker of two specific table data, we have to adjust the code to fix it. Is there an easy way to export excel for a particular permutation? Of course there is an answer, and easyPOI is recommended to deal with this problem. If there is a better solution, please leave a message to discuss.

Springboot Actual e-commerce project Mall4j (https://gitee.com/gz-yami/mall4j)

Java open source mall system

1. Easypoi profile

Easypoi function as the name easy, the main function is easy, so that a person who has not seen contact with POI can easily write Excel export,Excel template export,Excel import,Word template export, through simple annotations and template language (familiar expression grammar), complete the previous complex writing method. Easypoi official documentation

2. Why choose EasyPOI

The main value of easyPOi’s two functions, Excel template export, HTML preview. Excel template export, is to write a lot of repetitive code, export some complex arrangement of the table. HTML preview, sometimes we want to see some export effect first, do not want to export, need this HTML preview function. Of course, easyPOI also has some other functions, please go to esayPOI official documents to check it.

3. Introduce dependencies

Here we introduce the dependency of Spring Boot, the other can be viewed on the official website, refer to the introduction.

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.0. 0</version>
</dependency>
Copy the code

4. Export the Excel template

Start by defining an Excel template, as shown below:Then define the relevant variables, as shown in the figure

5. Code

 @RequestMapping("test")
    public void getExcelInfo(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {		
         // The path of the template in the project
        TemplateExportParams params = new TemplateExportParams(
                "doc/testExcelTemplate.xlsx");
               
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("code"."A20210101");
        map.put("applyTime"."2021-01-01");
        map.put("total".40000);
        map.put("company"."Pen Stealth Technology, Inc.");
        map.put("remark"."Test export");
        map.put("year"."2021");
        map.put("month"."04");
        map.put("day"."25");
        map.put("name"."JueYue");
        map.put("phone"."1879740 * * * *");
        List<Map<String, String>> listMap = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            Map<String, String> lm = new HashMap<String, String>(16);
            lm.put("amountType", i + "Test");
            lm.put("code"."A001");
            lm.put("subjectName"."Design");
            lm.put("projectName"."EasyPoi " + i + "Period");
            lm.put("fullName"."Ces project");
            lm.put("bankCard"."6216610200016587010");
            lm.put("bankName".Bank of China);
            lm.put("applyAmount", i * 10000 +"");
            lm.put("approvedAmount", i * 10000 +"");
            listMap.add(lm);
        }
        map.put("list", listMap);
        modelMap.put(TemplateExcelConstants.FILE_NAME, "Application for Special Disbursement _map.xls");
        modelMap.put(TemplateExcelConstants.PARAMS, params);
        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
        PoiBaseView.render(modelMap, request, response,
                TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }
Copy the code

Enter the path to access the download test, and the result is shown as follows:

This use we don’t have to write style, just need to create a template, fill in the data can be.

Springboot Actual e-commerce project Mall4j (https://gitee.com/gz-yami/mall4j)

Java open source mall system