EasyExcel的不確定表頭(根據數據生成表頭)的excel導出和二級表頭或多級表頭以及設置表頭的寬度自適應


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;
    }

多級表頭

 

private List<List<String>> getExcelHeader(String bigTitle, String nsrsbh, String ssyf) {
        List<List<String>> head = new ArrayList<>();
        head.add(Arrays.asList(bigTitle, "納稅人識別號:", "序號"));
        head.add(Arrays.asList(bigTitle, nsrsbh, "發票代碼"));
        head.add(Arrays.asList(bigTitle, "", "發票號碼"));
        head.add(Arrays.asList(bigTitle, "", "開票日期"));
        head.add(Arrays.asList(bigTitle, "", "銷方稅號"));
        head.add(Arrays.asList(bigTitle, "", "銷方名稱"));
        head.add(Arrays.asList(bigTitle, "所屬月份:", "金額"));
        head.add(Arrays.asList(bigTitle, ssyf, "稅額"));
        head.add(Arrays.asList(bigTitle, "", "有效稅額"));
        head.add(Arrays.asList(bigTitle, "", "勾選日期"));
        head.add(Arrays.asList(bigTitle, "", "發票類型"));
        head.add(Arrays.asList(bigTitle, "", "用途"));
        head.add(Arrays.asList(bigTitle, "", "發票狀態"));
        head.add(Arrays.asList(bigTitle, "", "管理狀態"));
        head.add(Arrays.asList(bigTitle, "單位:元", "信息來源"));
        return head;
    }

 效果

上圖的表頭闊度不是下面的的自適應代碼。

參考連接:https://blog.csdn.net/jie873440996/article/details/106399471?utm_medium=distribute.pc_relevant_download.none-task-blog-BlogCommendFromBaidu-9.nonecase&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-BlogCommendFromBaidu-9.nonecas

 

如果想通過實體類來設置二級表頭,可以通過@ExcelProperty(value={"一級表頭","二級表頭"}),多級表頭以此類推

EasyExcel.write(response.getOutputStream(), Download.class).sheet("導出模板").doWrite(data(byZtId));

 

通過調用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;
    }

總結

(1)表頭的List<List<String>>,最外面的List是一列單元格的表頭,里面的List的每一個元素代表這一列的每一行表頭

(2)數據的List<List<String>>,最外面的List是每一行的數據,里面的List放一行每一列的數據。

 參考鏈接:

 鏈接1

 鏈接2

 鏈接3

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM