1

我正在尝试使用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 个缺少依赖项,而我已经拥有了。这里出了什么问题?

4

5 回答 5

3

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

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

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

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

InputStream is = new FileInputStream(file);

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

于 2016-02-29T16:41:28.513 回答
3

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

在我更改代码后

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

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

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

这个错误消失了。

于 2016-09-07T15:19:18.090 回答
2

爪哇:

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>
于 2018-03-05T14:34:50.320 回答
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>
于 2016-01-25T10:40:41.227 回答
0

尝试了很多并解决了问题,但添加了以下用于解决此问题的依赖 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
于 2017-04-24T05:44:16.903 回答