Springboot integrates EasyPoi specified template to export excle

Springboot integrates EasyPoi to export excle

Excerpted from the EasyPoi tutorial: tutorial link

1. Add pom dependency

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.2.0</version>
</dependency>

2. Export

Specify template export

The advantage is that there is no need to configure a few merges, titles, fonts...

  • Space separation
  • Trinocular operation { {test? Obj:obj2}}
  • n: Indicates that this cell is a numeric type { {n:}}
  • le: represents the length { {le:()}} used in if/else { {le:()> 8? obj1: obj2}}
  • fd: formatting time { {fd:(obj;yyyy-MM-dd)}}
  • fn: formatted number { {fn:(obj;###.00)}}
  • fe: traverse the data, create row
  • !fe: Traverse the data without creating a row
  • $fe: Move down and insert, move the current line and the following lines down.size() lines, then insert
  • #fe: Horizontal traversal
  • v_fe: horizontal traversal value
  • !if: delete the current column { {!if:(test)}}
  • Single quotation marks indicate a constant value.'' For example, '1' then the output is 1
  • &NULL& space
  • ]] Newline character multi-line traversal export
  • sum:
    the writing of statistical data fe fe sign colon list data single element data (default t, you can not write) the first element
    { {$fe: maplist t t.id }}

Directory structure:
Insert picture description here
template content:
Insert picture description here
I have made the template myself, and will not upload attachments... Hey, I can only let the big guy look at it like this

java code:

package com.email.demo.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(value = "poi导出excle", tags = {
    
    "poi导出excle"})
@Slf4j
@Validated
@RestController
@RequestMapping("/poi")
public class EasyPoiController {
    
    

    @GetMapping("/export")
    public void export() throws Exception{
    
    

        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 =  new SimpleDateFormat("yyyyMMddHHmmss");
        String formatTimeStr = format.format(new Date());
        String formatTimeStr2 = format2.format(new Date());

        TemplateExportParams params = new TemplateExportParams("poi/指定模板poi导出.xlsx");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("unitSeal", "单位公章33333");
        map.put("date", formatTimeStr);
        map.put("createDate", formatTimeStr);
        map.put("money", 2000000.00);
        map.put("person", "老周");
        map.put("phone", "13111111111");

        List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 4; i++) {
    
    
            Map<String, String> lm = new HashMap<String, String>();
            lm.put("id", i + 1 + "");
            lm.put("nature", i * 10000 + "");   // 资金性质
            lm.put("num", UUID.randomUUID().toString().replace("-", ""));   // 编码
            lm.put("name", "A001" + i);
            lm.put("projectName", "项目名称" + i);
            lm.put("userName", "用户姓名" + i);
            lm.put("bankId", "银行账号" + i);
            lm.put("bankName", "开户银行" + i);
            lm.put("application", "200" + i);
            lm.put("approved", "500" + i);

            listMap.add(lm);
        }
        map.put("maplist", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
    
    
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/模板导出" + formatTimeStr2 +".xlsx");
        workbook.write(fos);
        System.out.println("导出excle文件成功!");
        fos.close();

    }
}

test

Swagger sends a request: it
Insert picture description here
is OK!

The console also prints out OK and
Insert picture description here
there is an extra file on the D disk:
Insert picture description here
Generated data:
Insert picture description here
also OK!!!

Welcome big guys to leave comments and learn together!!! Thanks!!!

===========================
Original article, reprinted with the source!

Guess you like

Origin blog.csdn.net/dayonglove2018/article/details/106800943