我正在尝试使用JXLS,这是一个用于创建excel文件的Java库,以及在Spring-MVC项目中使用它的其他操作。当我尝试从某些数据创建一个excel文件时,我收到以下错误:
错误日志:
java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:200)
at org.jxls.util.JxlsHelper.processTemplateAtCell(JxlsHelper.java:118)
at com.journaldev.spring.service.GroupNotesServiceImpl.saveGroupNotesToExcel(GroupNotesServiceImpl.java:917)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
代码:
@Override
public void saveGroupNotesToExcel(int msectionid){
List<GroupNotes> groupNotesList = this.groupNotesDAO.listGroupNotesBySectionId(msectionid);
try(InputStream is = GroupNotesServiceImpl.class.getResourceAsStream("/home/path/to/jls/test.xls")) {
try (OutputStream os = new FileOutputStream("/home/path/to/jls/output.xls")) {
Context context = new Context();
context.putVar("groupNotesList", groupNotesList);
JxlsHelper.getInstance().processTemplateAtCell(is, os, context, "Result!A1");
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
}
这是我的POM.xml与依赖项:
<!-- Excel dependencies-->
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.1</version>
</dependency>
我在网上检查了其他答案,但只有1个缺失依赖,我已经拥有了。这里出了什么问题?
我遇到的问题是出于某种原因,将资源作为流来获取是行不通的。它总是返回null。
更改我的代码以获取输入流来解决我的问题:
URL fileResource = this.getClass().getClassLoader().getResource("MyTemplate.xlsx");
File file = new File(fileResource.toURI());
InputStream is = new FileInputStream(file);
我的建议如果这不能解决你的问题,就是调试jxls代码,看看它返回null的位置,并确保其他输入都不为null。此错误仅表示无法构造有效的转换器,而不是类路径上不存在。我不知道为什么他们如此误导了这个例外。
我也遇到了这个问题,我的模板文件放在src / main / resources下。
我改变了代码之后
InputStream is = JxlsTest.class.getResourceAsStream("object_collection_template.xls");
(上面的代码无法读取模板文件)
InputStream is = JxlsTest.class.getResourceAsStream("/object_collection_template.xls");
这个错误消失了。
这是我阅读和编写Excel文档的依赖项,试试吧
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.6</version>
</dependency>
尝试了很多并解决了问题,但添加了以下用于解决此问题的依赖JAR列表
Java的:
List<Employee> employees = initEmployees();
try {
InputStream is = new FileInputStream(new File("/Users/Yasin/Downloads/template.xlsx"));
OutputStream os = new FileOutputStream(new File("/Users/Yasin/Downloads/formulas_output.xlsx"));
Context context = new Context();
context.putVar("employees", employees);
JxlsHelper.getInstance().processTemplateAtCell(is, os, context, "Result!A1");
} catch (Exception ex) {
ex.printStackTrace();
}
XML:
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.14</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>