当前位置: 首页 > news >正文

Java导出带图片的Excel

使用easypoi导出带图片的Excel,

引入依赖

依赖中着重要剔除可能会造成冲突的依赖,不剔除的话可能会报错

Exception in thread “main” java.lang.NoSuchFieldError: Class
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook does
not have member field ‘org.apache.xmlbeans.impl.schema.DocumentFactory Factory’

这个错误本质上是由于以下原因

  • XMLBeans 3.0+ 修改了内部工厂机制
  • POI 5.0+ 使用了新的类型系统
  • EasyPOI 可能携带了旧版本依赖

所以要保证项目依赖项里不要有冲突的依赖
在这里插入图片描述
以下是所使用到的所有依赖

<properties><java.version>24</java.version><poi.version>5.2.3</poi.version><easypoi.version>4.4.0</easypoi.version><xmlbeans.version>5.1.1</xmlbeans.version>
</properties><dependencies><!-- EasyPOI --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>${easypoi.version}</version><exclusions><exclusion><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId></exclusion><exclusion><groupId>org.apache.poi</groupId><artifactId>poi</artifactId></exclusion><exclusion><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId></exclusion><exclusion><groupId>org.apache.xmlbeans</groupId><artifactId>xmlbeans</artifactId></exclusion></exclusions></dependency><!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi.version}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi.version}</version></dependency><!-- XMLBeans --><dependency><groupId>org.apache.xmlbeans</groupId><artifactId>xmlbeans</artifactId><version>${xmlbeans.version}</version></dependency><!-- 其他必要依赖 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.23.0</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency>
</dependencies>

实体类

实体类中着重注意的是图片字段一定要带有type=2配置属性

package com.xmliu.easypoidemo;import cn.afterturn.easypoi.excel.annotation.Excel;public class Employee {@Excel(name = "姓名", orderNum = "0", width = 15)private String name;@Excel(name = "部门", orderNum = "1", width = 20)private String department;// 关键图片配置, savePath = "src/main/resources/images/"@Excel(name = "照片", orderNum = "2", type = 2, width = 20, height = 20)private String photoPath;// 必须有无参构造public Employee() {}public Employee(String name, String department, String photoPath) {this.name = name;this.department = department;this.photoPath = photoPath;}// getter和setterpublic String getName() {return name;}public void setName(String name) {this.name = name;}public String getDepartment() {return department;}public void setDepartment(String department) {this.department = department;}public String getPhotoPath() {return photoPath;}public void setPhotoPath(String photoPath) {this.photoPath = photoPath;}
}

导出Excel

该导出方法提供了两种图片加载方式,一个是本地磁盘图片,一个是项目内部图片,当然也可以是在线图片url

package com.xmliu.easypoidemo;import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.Workbook;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class EasyPoiDemo {public static void main(String[] args) {// 模拟数据List<Employee> employees = new ArrayList<>();
//        employees.add(new Employee("张三", "技术部", "src/main/resources/images/huoche.jpg"));
//        employees.add(new Employee("李四", "市场部", "src/main/resources/images/echarts.png"));
//        employees.add(new Employee("王五", "人事部", "src/main/resources/images/wangwu.jpeg"));employees.add(new Employee("张三", "技术部", "/Users/zhangwei/Downloads/huoche.jpg"));employees.add(new Employee("李四", "市场部", "/Users/zhangwei/Downloads/echarts.png"));employees.add(new Employee("王五", "人事部", "/Users/zhangwei/Downloads/wangwu.jpeg"));// 导出ExcelexportWithImages(employees, "员工信息表" + System.currentTimeMillis()+".xlsx");}public static void exportWithImages(List<Employee> data, String fileName) {// 1. 配置导出参数ExportParams params = new ExportParams("员工信息表", "员工数据", ExcelType.XSSF);// 2. 生成工作簿Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, data);// 3. 写入文件try (FileOutputStream out = new FileOutputStream(fileName)) {workbook.write(out);System.out.println("Excel导出成功,文件保存在: " + new File(fileName).getAbsolutePath());} catch (IOException e) {System.err.println("Excel导出失败: " + e.getMessage());e.printStackTrace();}}}

导出结果

在这里插入图片描述
从上图中可以看出导出的Excel并没有图片,这个问题确实很奇怪
但是我们通过压缩软件打开该Excel发现
在这里插入图片描述
三张图片确实已经存在,只是没显示到Excel单元格中,该问题还需进一步研究原因与解决方法,如若哪位朋友知道,还望不吝赐教,在下不胜感激。
以下是控制台日志

已连接到地址为 ''127.0.0.1:55220',传输: '套接字'' 的目标虚拟机
Excel导出成功,文件保存在: /Users/zhangwei/IdeaProjects/easypoi-demo/员工信息表1745986971020.xlsx
已与地址为 ''127.0.0.1:55220',传输: '套接字'' 的目标虚拟机断开连接进程已结束,退出代码为 0

相关文章:

  • ERP系统为何沦为“电子台账“?
  • TCP和UDP传输层协议
  • 归并排序算法
  • 第六章 流量特征分析-常见攻击事件 tomcat wp
  • Nginx — http、server、location模块下配置相同策略优先级问题
  • react-11使用vscode开发react相关扩展插件(相关的快捷生成)
  • 用Playwright自动化网页测试,不只是“点点点”
  • 大型连锁酒店集团数据湖应用示例
  • 深度解析 MindTorch:无缝迁移 PyTorch 到 MindSpore 的高效工具
  • Centos离线安装Docker(无坑版)
  • 硬件工程师面试常见问题(10)
  • [250430] Kali Linux 存储库密钥丢失导致所有用户无法正常更新 APT
  • 如何使用C语言手搓斐波那契数列?
  • 【数学建模国奖速成系列】优秀论文绘图复现代码(二)
  • OCR身份证识别(正反面)_个人证照OCR识别_开放API接口使用指南
  • AI重构家居营销新范式:DeepSeek如何破解行业流量与转化困局?
  • 在pycharm中创建Django项目并启动
  • 在 API 模拟阶段:Apipost vs. Faker.js vs. Postman —— 为什么 Apipost 是最优选择
  • 科学数据可视化工具库visIt安装和使用
  • 「动态规划::背包」01背包 / AcWing 2(C++)
  • 美国第一季度经济环比萎缩0.3%
  • 国家医保局副局长颜清辉调任人社部副部长
  • 比黄油年糕热量还高,这个火爆全网的甜品劝你慎吃
  • 中国人民解放军南部战区位南海海域进行例行巡航
  • 中国人保不再设监事会,国寿集团未再设置监事长职务
  • 上海“模速空间”:将形成人工智能“北斗七星”和群星态势