如何使用java生成excel报表并解决运行时三个class not found问题

1.先网上找个简易 简洁的基础案例
https://blog.youkuaiyun.com/fanrenxiang/article/details/81066497

试着CV 一下在新建的项目run一下
好了,不出所料报错了

报错一:
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
https://www.concretepage.com/questions/202
解决方法:
downlaod xmlbeans-2.6.0.jar或将POM设置为

<dependency> 
    <groupId> org.apache.xmlbeans </ groupId> 
    <artifactId> xmlbeans </ artifactId> 
    <version> 2.6.0 </ version> 
  </ dependency>

解决上述问题,
报错二:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections4.ListValuedMap
https://blog.youkuaiyun.com/you23hai45/article/details/70197383

解决方法:
(1)将commons-collections4-4.1.jar复制到lib目录
(2)如果是Maven项目,可以在pom.xml中添加

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>

然后,报错三:
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook$Factory 没找到
https://stackoverflow.com/questions/40038041/java-lang-noclassdeffounderror-org-openxmlformats-schemas-spreadsheetml-x2006-m

添加此依赖项:

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>

同时,值得注意的问题是版本号兼容:
https://www.cnblogs.com/ae6623/p/4416424.html
由于poi使用的poi-ooxml-schemas是ooxml-schemas的精简版,所以在was服务器上导出Excel可能会报错,tomcat下面使用前者是没有问题的,此时需要统一jar包版本为ooxml-schemas.1.1.jar,这个ooxml-schemas有两个版本的jar包,一个是 POI 3.7,之前版本使用的ooxml-schemas-1.0.jar.另一个就是poi3.7以后版本使用的ooxml-schemas-1.1.jar,官网也说了这个问题:http://poi.apache.org/faq.html#faq-N10025,

Java生成excel报表文件时间如何处理?
https://bbs.youkuaiyun.com/topics/392287466

cell.setCellValue((Date) cellData);
String dateFormat="yyyy-MM-dd";
CellStyle cellStyle = createCellStyle(dateFormat);
cell.setCellStyle(cellStyle)

–以下是createCellStyle对应方法

public CellStyle createCellStyle(String format){
CellStyle cellStyle = workbook.createCellStyle();
CreationHelper creationHelper = workbook.getCreationHelper();
short dataFormat = creationHelper.createDataFormat().getFormat(format);
cellStyle.setDataFormat(dataFormat);
return cellStyle;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值