Spring,JXLS:XLS转换器不加载,尽pipe存在依赖关系。

我正在尝试使用JXLS ,这是一个Java库,用于在Spring-MVC项目中创buildexcel文件和其他操作。 当我尝试从某些数据中创build一个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> 

我查了网上的其他答案,但只有一个缺less的依赖,我已经有了。 这里怎么了?

我遇到的问题是出于什么原因,以stream的forms获取资源是行不通的。 它总是会返回null。

改变我的代码,以获得inputstream来解决我的问题:

 URL fileResource = this.getClass().getClassLoader().getResource("MyTemplate.xlsx"); File file = new File(fileResource.toURI()); InputStream is = new FileInputStream(file); 

我的build议,如果这不解决你的问题,是debugging到jxls代码,看看它返回null,并确保没有其他input在这一点上是空的。 这个错误只是意味着一个有效的变换器不能被构造,而不是类path上不存在的。 我不知道为什么他们这样做会让人误解。

我也遇到过这个问题,我的模板文件放在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列表来解决这个问题

  1. 1.commons-codec-1.10.jar 2.commons-collections4-4.1.jar 3.commons-jexl-2.1.1.jar 4.commons-logging-1.2.jar 5.jxls-2.4.0.jar 6.jxls -jexcel-1.0.6.jar 7.jxls-poi-1.0.12.jar 8.jxls-reader-2.0.2.jar 9.poi-3.14.jar 10.poi-ooxml-3.15.jar 11.slf4j- API-1.6.6.jar