EasyExcel的使用--写

时间:2025-02-07 13:22:31

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。

今天根据官网(/easyexcel/doc/read)指导,试了一个简单的写入例子,如下。

了解常见API的话,访问地址:常见api · 语雀 ()

1.创建一个写入实体:


@Data
//以下注解设置列宽行高
@ContentRowHeight(10)
@HeadRowHeight(20)
@ColumnWidth(25)
//以下注解设置样式,可以放到字段属性上。
// 头背景设置成红色 ()
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 10)
// 头字体设置成20
@HeadFontStyle(fontHeightInPoints = 20)
// 内容的背景设置成绿色 ()
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 17)
// 内容字体设置成20
@ContentFontStyle(fontHeightInPoints = 20)
// 合并单元格:将第6-7行的2-3列合并成一个单元格
// @OnceAbsoluteMerge(firstRowIndex = 5, lastRowIndex = 6, firstColumnIndex = 1, lastColumnIndex = 2)
public class ExportData {
    //@ExcelProperty("名称")
    //以下写法包含复杂表头,和字段写入excel的索引
    @ExcelProperty(value = {"主标题", "名称"}, index = 0, converter = )
    //加入以下注解可以合并单元格
    //@ContentLoopMerge(eachRow = 2)
    private String string;
    //@ExcelProperty("日期")
    //以下写法包含复杂表头,和字段写入excel的索引
    @ExcelProperty(value = {"主标题", "日期"}, index = 1)
    @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
    private Date date;
    //@ExcelProperty("值")
    //以下写法包含复杂表头,和字段写入excel的索引
    @ExcelProperty(value = {"主标题", "值"}, index = 2)
    @NumberFormat("#.##%")
    //以下注解设置列宽
    @ColumnWidth(50)
    private Double doubleData;
    /**
     * 忽略这个字段
     */
    @ExcelIgnore
    private String ignore;
}

2.写入测试的main方法:

/**
 * 写入excel测试方法
 */
public class ExportMain {
    public static void main(String[] args) {
        //写入excel例子
        String fileName = "d:\\";
        (fileName, ).sheet("模板").doWrite(data());

        // 根据用户传入字段 忽略date
        Set<String> excludeColumnFiledNames = new HashSet<String>();
        ("date");
        (fileName, ).excludeColumnFiledNames(excludeColumnFiledNames).sheet("模板")
                .doWrite(data());

        // 根据用户传入字段 只要导出date
        Set<String> includeColumnFiledNames = new HashSet<String>();
        ("date");
        (fileName, ).includeColumnFiledNames(includeColumnFiledNames).sheet("模板")
                .doWrite(data());

        //以下内容可以写入不同的sheet和不同的内容,可以用于分页
        ExcelWriter excelWriter = null;
        try {
            excelWriter = (fileName).build();
            for (int i = 0; i < 5; i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。
                // ExportData类可以每次改变。
                WriteSheet writeSheet = (i, "模板" + i).head().build();
                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                List<ExportData> data = data();
                (data, writeSheet);
            }
        } finally {
            // 千万别忘记finish 会关闭流
            if (excelWriter != null) {
                ();
            }
        }

        //根据模板导入
        String templateFileName = "d:\\";
        (fileName, ).withTemplate(templateFileName).sheet().doWrite(data());

        //自定义样式
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        (());
        WriteFont headWriteFont = new WriteFont();
        ((short)20);
        (headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        (FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
        (());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        ((short)20);
        (contentWriteFont);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        (fileName, ).registerWriteHandler(horizontalCellStyleStrategy).sheet("模板")
                .doWrite(data());

        //合并单元格案例(注解合并和此合并是两种方式)
        // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度,所以就第一列会合并。当然其他合并策略也可以自己写
        LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(2, 0);
        (fileName, ).registerWriteHandler(loopMergeStrategy).sheet("模板").doWrite(data());

        //使用table写入
        try {
            excelWriter = (fileName, ).build();
            // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
            WriteSheet writeSheet = ("模板").needHead().build();
            // 这里必须指定需要头,table 会继承sheet的配置,sheet配置了不需要,table 默认也是不需要
            WriteTable writeTable0 = (0).needHead().build();
            WriteTable writeTable1 = (1).needHead().build();
            // 第一次写入会创建头
            (data(), writeSheet, writeTable0);
            // 第二次写如也会创建头,然后在第一次的后面写入数据
            (data(), writeSheet, writeTable1);
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                ();
            }
        }

        //动态放入头信息
        (fileName)
                // 这里放入动态头
                .head(head()).sheet("模板")
                .doWrite(data());

        //自定义拦截器写法
        (fileName, ).registerWriteHandler(new CustomSheetWriteHandler())
                .registerWriteHandler(new CustomCellWriteHandler()).sheet("模板").doWrite(data());


        //添加批注,使用批注拦截器
        //这里要注意inMemory 要设置为true,才能支持批注。目前没有好的办法解决 不在内存处理批注。这个需要自己选择。
        (fileName, ).inMemory().registerWriteHandler(new CommentWriteHandler())
                .sheet("模板").doWrite(data());
    }

    /**
     * 组装要导入的数据
     * @return
     */
    private static List<ExportData> data() {
        List<ExportData> list = new ArrayList<ExportData>();
        for (int i = 0; i < 10; i++) {
            ExportData data = new ExportData();
            ("字符串" + i);
            (new Date());
            (0.56);
            (data);
        }
        return list;
    }

    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        ("字符串" + ());
        List<String> head1 = new ArrayList<String>();
        ("数字" + ());
        List<String> head2 = new ArrayList<String>();
        ("日期" + ());
        (head0);
        (head1);
        (head2);
        return list;
    }
}

3.图片写入例子:

/**
 * 图片写入实体
 */
@Data
@ContentRowHeight(100)
@ColumnWidth(100 / 8)
public class ImageData {
    private File file;
    private InputStream inputStream;
    /**
     * 如果string类型 必须指定转换器,string默认转换成string
     */
    @ExcelProperty(converter = )
    private String string;
    private byte[] byteArray;
    /**
     * 根据url导出
     */
    private URL url;
}
public static void main(String[] args) throws Exception{
        String fileName = "d:\\";
        // 如果使用流 记得关闭
        InputStream inputStream = null;
        try {
            List<ImageData> list = new ArrayList<ImageData>();
            ImageData imageData = new ImageData();
            (imageData);
            String imagePath = "";
            // 放入五种类型的图片 实际使用只要选一种即可
            ((new File(imagePath)));
            (new File(imagePath));
            (imagePath);
            inputStream = (new File(imagePath));
            (inputStream);
            (new URL("/alibaba/easyexcel/master/src/test/resources/converter/"));
            (fileName, ).sheet().doWrite(list);
        } finally {
            if (inputStream != null) {
                ();
            }
        }
    }

4.自定义拦截器

/**
 * 自定义拦截器
 */
public class CustomCellWriteHandler implements CellWriteHandler {

    private static final Logger LOGGER = ();

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                 Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
                                Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 这里可以对cell进行任何操作
        ("第{}行,第{}列写入完成。", (), ());
        if (isHead && () == 0) {
            CreationHelper createHelper = ().getWorkbook().getCreationHelper();
            Hyperlink hyperlink = ();
            ("/alibaba/easyexcel");
            (hyperlink);
        }
    }

}
/**
 * 自定义拦截器.对第一列第一行和第二行的数据新增下拉框,显示 测试1 测试2
 */
public class CustomSheetWriteHandler implements SheetWriteHandler {

    private static final Logger LOGGER = ();

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        ("第{}个Sheet写入成功。", ());

        // 区间设置 第一列第一行和第二行的数据。由于第一行是头,所以第一、二行的数据实际上是第二三行
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 2, 0, 0);
        DataValidationHelper helper = ().getDataValidationHelper();
        DataValidationConstraint constraint = (new String[] {"测试1", "测试2"});
        DataValidation dataValidation = (constraint, cellRangeAddressList);
        ().addValidationData(dataValidation);
    }
}

5.添加批注拦截器

/**
 * 添加批注拦截器
 */
public class CommentWriteHandler extends AbstractRowWriteHandler {

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            Sheet sheet = ();
            Drawing<?> drawingPatriarch = ();
            // 在第一行 第二列创建一个批注
            Comment comment =
                    (new XSSFClientAnchor(0, 0, 0, 0, (short)1, 0, (short)2, 1));
            // 输入批注信息
            (new XSSFRichTextString("创建批注!"));
            // 将批注添加到单元格对象中
            (0).getCell(1).setCellComment(comment);
        }
    }

}

中写入

@GetMapping("downloadFailedUsingJson")
    public void downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        try {
            ("application/");
            ("utf-8");
            // 这里可以防止中文乱码 当然和easyexcel没有关系
            String fileName = ("测试", "UTF-8").replaceAll("\\+", "%20");
            ("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            ((), ).autoCloseStream().sheet("模板")
                .doWrite(data());
        } catch (Exception e) {
            // 重置response
            ();
            ("application/json");
            ("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            ("status", "failure");
            ("message", "下载文件失败" + ());
            ().println((map));
        }
    }