使用hutool导出excel,表头的列不固定,包括导出图片,合并表头

1,944 阅读1分钟

依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.7</version>
</dependency>

造数据

ArrayList<Map> maps = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    HashMap<String, Object> map = new HashMap<>();
    map.put("code2", "1001");
    map.put("code3", "张三");
    map.put("code4", 23);
    map.put("code8", "男");
    map.put("code9", "陕西西安rewrfewdsgfasgf");
    map.put("code1", "2020-09-01");
    map.put("code5", "22.33");
    maps.add(map);
}

使用map定义标题

 ```
cn.hutool.poi.excel.ExcelWriter writer = ExcelUtil.getWriter();
```
// 需要合并的行

writer.merge(0,0,0,11, "第一行,1至11列,使用默认标题样式", true);
writer.merge(1,1,0,5, "第二行,0至5列,不使用默认标题样式", false);
writer.merge(1,1,6,11, "第二行,6至11列", false);

//跳过前3行
writer.passRows(3);

自定义标题
```
LinkedHashMap<String, String> map = new LinkedHashMap<>();
map.put("code1","标题1");
map.put("code2","标题2");
map.put("code3","标题3");
map.put("code4","标题4(");
map.put("code5","标题5");
map.put("code6","标题6"  );
map.put("code7","标题7"  );
map.put("code8","标题8");
map.put("code9","备注");
writer.setHeaderAlias(map);
```

添加图片


private void setImage(TisResultPublish publish, short row, ExcelWriter writer) {
    if (StringUtil.isNotNull(publish.getLogo())) {
        Sheet sheet = writer.getSheet();
        Drawing drawingPatriarch = sheet.createDrawingPatriarch();
        //设置图片单元格位置
        ClientAnchor anchor = drawingPatriarch.createAnchor(0, 0, 1023, 250,
                (short) 0, 0, row, 0);
        //随单元格改变位置和大小
        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
       // byte[] pictureData = FileUtil.readBytes("C:\fas\file\导出2.jpg");
        byte[] pictureData = FileUtil.readBytes(baseFilePath+ Constants.SAVE_DIR+ File.separator+publish.getLogo());
        //添加图片
        int pictureIndex = sheet.getWorkbook().addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_JPEG);
        drawingPatriarch.createPicture(anchor, pictureIndex);
    }
}

设置样式

private void setStyle(List<Map> maps, ExcelWriter writer) {
    CellStyle headCellStyle = writer.getHeadCellStyle();

    Font font2 = writer.createFont();
    font2.setBold(true);
    font2.setFontHeightInPoints((short) 14);
    font2.setFontName("微软雅黑");
    headCellStyle.setFont(font2);
    headCellStyle.setWrapText(true);
    writer.write(maps, true);

    writer.setRowHeight(0, 50);
    writer.setRowHeight(1, 30);
    writer.setRowHeight(2, 30);
    writer.setRowHeight(3, 30);
    writer.setRowHeight(4, 30);
    writer.setRowHeight(5, 30);
    writer.setRowHeight(6, 30);
    writer.setRowHeight(7, 30);
    writer.setRowHeight(8, 30);
    writer.setRowHeight(9, 30);
    writer.setRowHeight(10, 30);
    writer.setRowHeight(11, 50);

    writer.setColumnWidth(-1, 20);
    writer.setColumnWidth(0, 12);
    writer.setColumnWidth(1, 12);
    writer.setColumnWidth(2, 12);

    CellStyle cellStyle2 = writer.getCellStyle();

    cellStyle2.setVerticalAlignment(VerticalAlignment.CENTER);
    cellStyle2.setAlignment(HorizontalAlignment.CENTER);
    Font font3 = writer.createFont();
    font3.setBold(false);
    font3.setFontHeightInPoints((short) 12);
    font3.setFontName("微软雅黑");
    cellStyle2.setFont(font3);
    cellStyle2.setWrapText(false);
}

导出

try {
    OutputStream out = response.getOutputStream();
    log.info("export===========" + newPath + File.separator + newFileName);
    writer.flush(out, true);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    writer.close();
}