EasyPoi 导出文本图片Excel

一.参考官网文档:easypoi文档
二.导入easypoi的maven依赖

 <dependency>
     <groupId>cn.afterturn</groupId> 
     <artifactId>easypoi-base</artifactId> 
     <version>3.2.0</version>
 </dependency>
 <dependency> 
     <groupId>cn.afterturn</groupId> 
     <artifactId>easypoi-web</artifactId>
     <version>3.2.0</version> 
 </dependency> 
 <dependency>
     <groupId>cn.afterturn</groupId> 
     <artifactId>easypoi-annotation</artifactId> 
     <version>3.2.0</version>
 </dependency>

三.生成Excel准备工作
3.1 对象实体添加注解

public class TBreakRule implements Serializable {
    /**
     * id
     */
    @Excel(name = "编号",orderNum = "1")
    private Integer id;

    @Excel(name = "违停编号",width = 25,orderNum = "2")
    private String breakCode;

    /**
     * 车牌号
     */
    @Excel(name = "车牌号",orderNum = "3")
    private String plateNumber;

    /**
     * 地址
     */
    @Excel(name = "地址",width = 60,orderNum = "4")
    private String address;

    /**
     * 图片
     */
    private String picture;


    @Excel(name = "车辆正面照片",type = 2,width = 50,height = 60,orderNum = "5")
    private String picture1;
    @Excel(name = "车辆左侧照片",type = 2,width = 50,height = 60,orderNum = "5")
    private  String picture2;
    @Excel(name = "车辆右侧照片",type = 2,width = 50,height = 60,orderNum = "5")
    private String picture3;

    /**
     * 违章信息
     */
    @Excel(name = "违章信息",orderNum = "6")
    private String breakInfo;

    /**
     * 车牌颜色
     */
    @Excel(name = "车牌颜色",orderNum = "7")
    private String plateColor;

    /**
     * 01:大型车。02:小型车 07:2,3轮摩托车,08:轻便摩托车。13:农用运输车
     */
    @Excel(name = "车辆类型",replace = {"大型车_01","小型车_02","摩托车_07","轻便摩托车_08","农用运输车_13"},orderNum = "8")
    private String carType;

    /**
     * 车颜色
     */
    @Excel(name = "车身颜色",suffix = "色",orderNum = "9" )
    private String carCo
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在使用 EasyPoi 导出 Excel 文件时插入图片,您需要使用 `ExcelExportUtil` 类和 `ImageExportUtil` 类。 以下是一个示例代码: ```java public class ExportWithImageTest { public static void main(String[] args) throws IOException { // 准备数据 List<User> userList = new ArrayList<>(); userList.add(new User("John", 25, getImageBytes("john.png"))); userList.add(new User("Jane", 30, getImageBytes("jane.png"))); // 导出 Excel 文件 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, userList); // 插入图片 for (int i = 0; i < userList.size(); i++) { byte[] imageBytes = userList.get(i).getImageBytes(); if (imageBytes != null) { ImageExportUtil.createImage(workbook, (byte[]) imageBytes, i, 2, 3, 5); } } // 保存文件 FileOutputStream outputStream = new FileOutputStream("users.xlsx"); workbook.write(outputStream); outputStream.close(); } private static byte[] getImageBytes(String filename) throws IOException { InputStream inputStream = ExportWithImageTest.class.getResourceAsStream(filename); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } return outputStream.toByteArray(); } } ``` 在上面的代码中,我们首先创建了一个包含用户姓名、年龄和头像图片字节数组的列表。然后,我们使用 `ExcelExportUtil` 导出 Excel 文件,并遍历用户列表以插入图片。 请注意,我们需要将图片插入到正确的单元格中,以便它们与相应的用户匹配。在上面的示例中,我们将图片插入到第二列的第四行和第三列的第四行。 另外,我们还需要使用 `ImageExportUtil` 类的 `createImage` 方法将图片插入到工作簿中。该方法接受一个工作簿对象、一个字节数组、一个行索引、一个列索引和四个单元格偏移量,以确定图片应该插入到哪个单元格中。 最后,我们将工作簿保存到文件中。在上面的示例中,我们将文件保存为 `users.xlsx`。 请注意,如上所述,该示例仅适用于在单元格中插入静态图片的情况。如果您需要在单元格中插入动态图像或其他更高级的功能,可能需要使用其他库或更复杂的技术实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值