easyexcel填充复杂表格并导出以及使用easyexcel遇到的坑

读取模板时出现Your file appears not to be a valid OLE2 document
解决办法:另存为97-03年的xls格式即可
easyexcel填充复杂表格并导出以及使用easyexcel遇到的坑_第1张图片

准备一个填充模板
easyexcel填充复杂表格并导出以及使用easyexcel遇到的坑_第2张图片
其中{xxx}为普通变量,{.xxx} 的为list变量

准备一个实体类

public class vo {
     
    private String terminal;
    private String color;
    private String number;
    private String zq;
    private String dx;
    private String zt;
    private String zd;
    private String rk;

    public String getTerminal() {
     
        return terminal;
    }

    public void setTerminal(String terminal) {
     
        this.terminal = terminal;
    }

    public String getColor() {
     
        return color;
    }

    public void setColor(String color) {
     
        this.color = color;
    }

    public String getNumber() {
     
        return number;
    }

    public void setNumber(String number) {
     
        this.number = number;
    }

    public String getZq() {
     
        return zq;
    }

    public void setZq(String zq) {
     
        this.zq = zq;
    }

    public String getDx() {
     
        return dx;
    }

    public void setDx(String dx) {
     
        this.dx = dx;
    }

    public String getZt() {
     
        return zt;
    }

    public void setZt(String zt) {
     
        this.zt = zt;
    }

    public String getZd() {
     
        return zd;
    }

    public void setZd(String zd) {
     
        this.zd = zd;
    }

    public String getRk() {
     
        return rk;
    }

    public void setRk(String rk) {
     
        this.rk = rk;
    }
}

一个处理导出的controller类

@RequestMapping("/expor")
    public String exporExcel(HttpServletResponse response) throws IOException {
     
        OutputStream outputStream = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment; filename=" + "catagory.xls");
        response.setContentType("application/msexcel;charset=UTF-8");//设置类型
        response.setHeader("Pragma", "No-cache");//设置头
        response.setHeader("Cache-Control", "no-cache");//设置头
        response.setDateHeader("Expires", 0);//设置日期头
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(ResourceUtils.getFile("classpath:excelTemplates/template.xls")).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        List<vo> list =new ArrayList<vo>();
        vo v = new vo();
        v.setColor("1");
        v.setDx("1");
        v.setNumber("1");
        v.setRk("1");
        v.setTerminal("1");
        v.setZq("1");
        v.setZt("1");
        v.setRk("1");
        list.add(v);
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        //填充两行list
        excelWriter.fill(list, fillConfig, writeSheet);
        excelWriter.fill(list, fillConfig, writeSheet);
        //填充普通变量
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bussinessRequest", "1");
        map.put("linkman", "1");
        map.put("linkphone", "1");
        map.put("orderdate", "1");
        map.put("leaddate", "1");
        map.put("entrct", "1");
        map.put("comstomer", "1");
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
        outputStream.flush();
        response.getOutputStream().close();
        return "system/test/tableTest";
    }

填充后并导出的效果,因为controller中忘记设置zd的值所以图中对应zd的表格并没有值
easyexcel填充复杂表格并导出以及使用easyexcel遇到的坑_第3张图片
更多easyexcel用法可以查看https://www.yuque.com/easyexcel/doc/fill

你可能感兴趣的:(java)