码迷,mamicode.com
首页 > 其他好文 > 详细

EasyExcel的不确定表头(根据数据生成表头)的excel导出和二级表头以及设置表头的宽度自适应

时间:2020-07-10 18:49:09      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:override   mapping   out   abstract   compare   new   ref   turn   style   

1、效果如图

技术图片

2、Controller

@GetMapping("/exportExcel")
    public void exportExcel(@RequestParam(value = "menu") String menu,
                            @RequestParam(value = "dwflglId") int dwflglId,
                            @RequestParam(value = "qjCode") String qjCode,
                            HttpServletResponse response) throws IOException {
      //内容集合
        List<ZjlrysIndexView> jthz = getJthz(dwflglId, qjCode);
        //表头集合
        List<ZjlrysTableHead> header = getHeaderOfHz(dwflglId, qjCode);
        response.setHeader("Content-Disposition", "attachment; filename=Zjlryshz.xlsx");
        // 响应类型,编码
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String bigTitle = "资金流入预算表";
        EasyExcel.write(response.getOutputStream()).head(getExcelHeader(header, bigTitle)).registerWriteHandler(new Custemhandler()).sheet("资金流入预算导出模板").doWrite(getExportData(jthz));
    }

通过getExcelHeader(header, bigTitle)得到表头集合

private List<List<String>> getExcelHeader(List<ZjlrysTableHead> header, String bigTitle) {
        List<List<String>> head = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        head0.add(bigTitle);
        head0.add("资金项目");
        head.add(head0);
        head0 = new ArrayList<>();
        head0.add(bigTitle);
        head0.add("行次");
        head.add(head0);
        for (var h : header) {
            head0 = new ArrayList<>();
            head0.add(bigTitle);
            head0.add(h.getName());
            head.add(head0);
        }
        return head;
    }

通过调用registerWriteHandler来设置表格样式,我这里设置只是设置表头的自适应宽度,创建一个类来继承AbstractColumnWidthStyleStrategy抽象类

package cn.xxxx.xxxx.xxx.utils;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Custemhandler extends AbstractColumnWidthStyleStrategy {
    private static final int MAX_COLUMN_WIDTH = 255;
    //the maximum column width in Excel is 255 characters

    public Custemhandler() {
    }

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (isHead && cell.getRowIndex() != 0) {
            int columnWidth = cell.getStringCellValue().getBytes().length;
            if (columnWidth > MAX_COLUMN_WIDTH) {
                columnWidth = MAX_COLUMN_WIDTH;
            } else {
                columnWidth = columnWidth + 3;
            }
            writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);

        }
    }


    private final static Logger logger = LoggerFactory.getLogger(Custemhandler.class);
}

最后在doWrite()里面放要导出的数据即可

 private List<List<String>> getExportData(List<ZjlrysIndexView> jthz) {
        List<List<String>> sumList = new ArrayList<>();
        for (var row : jthz) {
            List<String> list = new ArrayList<>();
            list.add(row.getZjxm());
            list.add(String.valueOf(row.getZbY()));
            for (var xm : row.getZjlrysXms()) {
                if (xm.getId() == null) {
                    list.add("");
                } else {
                    if (xm.getZjlrysb().getXsValue() == null || xm.getZjlrysb().getXsValue().compareTo(new BigDecimal("0")) == 0) {
                        list.add("");
                    } else {
                        list.add(xm.getZjlrysb().getXsValue().toString());
                    }
                }
            }
            sumList.add(list);
        }
        return sumList;
    }

无论表头还是内容,传给easyexcel的都是List<List<String>>这种形式的集合,可以说是二维的吧,外面的List相当于row,里面的List相当于col

 参考链接:

 链接1

 链接2

 链接3

 

EasyExcel的不确定表头(根据数据生成表头)的excel导出和二级表头以及设置表头的宽度自适应

标签:override   mapping   out   abstract   compare   new   ref   turn   style   

原文地址:https://www.cnblogs.com/pzw23/p/13280707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!