Spring,JXLS:XLS转换器未加载,即使存在依赖关系。

时间:2016-01-25 10:22:24

标签: java excel spring maven jxls

我正在尝试JXLS,这是一个用于在Spring-MVC项目中创建excel文件和其他操作的Java库。当我尝试使用某些数据创建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个缺少依赖,我已经拥有了。这里出了什么问题?

5 个答案:

答案 0 :(得分:3)

我遇到的问题是出于某种原因,将资源作为流来获取是行不通的。它总是返回null。

更改我的代码以获取输入流解决了我的问题:

URL fileResource = this.getClass().getClassLoader().getResource("MyTemplate.xlsx");

File file = new File(fileResource.toURI());

InputStream is = new FileInputStream(file);

如果这不能解决您的问题,我的建议是调试jxls代码并查看它返回null的位置,并确保其他输入在该点都不为空。此错误仅表示无法构造有效的转换器,而不是类路径上不存在。我不知道为什么他们让例外如此误导。

答案 1 :(得分:3)

我也遇到了这个问题,我的模板文件放在src / main / resources下。

我从

更改代码后
InputStream is = JxlsTest.class.getResourceAsStream("object_collection_template.xls");

(上面的代码无法读取模板文件) 到

InputStream is = JxlsTest.class.getResourceAsStream("/object_collection_template.xls");

此错误消失。

答案 2 :(得分:1)

以下是我阅读和编写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>

答案 3 :(得分:0)

尝试了很多并解决了问题,但添加了以下用于解决此问题的依赖JAR列表

  1. 1.commons编解码器-1.10.jar 2.commons-collections4-4.1.jar 3.commons-JEXL-2.1.1.jar 4.commons测井-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阅读器 - 2.0.2.jar 9.poi-3.14.jar 10.poi-OOXML-3.15.jar 11.slf4j-API-1.6.6.jar

答案 4 :(得分:0)

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>