EasyExcel 动态表头模板下载

发布时间 2023-07-04 08:44:32作者: 南国的刘新
 List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("VIN");
        list.add(head0);

        if("1".equals(type)){
            List<String> head1 = new ArrayList<String>();
            head1.add("预计时间");
            list.add(head1);
        }

        try {
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            WriteFont contentWriteFont = new WriteFont();
            contentWriteFont.setColor(IndexedColors.RED.index);
            headWriteCellStyle.setWriteFont(contentWriteFont);

            EasyExcel.write(response.getOutputStream())
                    .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, new WriteCellStyle()))
                    // 这里放入动态头
                    .head(list).sheet("模板")
                    // 当然这里数据也可以用 List<List<String>> 去传入
                    .doWrite(null);
        } catch (IOException ex) {
            ex.printStackTrace();
        }