在jXLS 1+中,我使用.XLSX模板和输出文件;它运作良好。但是在Jxls 2+中没有。因此,我准备了模板并重写了代码。如果我使用的是.XLS,那么效果很好;如果我将模板更改为.XLSX,则会出现以下问题:
2018-06-26 11:56:33,827 ERROR [main] TransformerFactory - Method createTransformer of org.jxls.transform.poi.PoiTransformer class thrown an Exception
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:35)
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:381)
at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:173)
...
Caused by: java.lang.ClassNotFoundException: org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument$Factory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more
2018-06-26 11:56:33,830 ERROR [main] ExcelBuilder - Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:383)
at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:173)
...
似乎不支持.XLSX,或者我需要另一个库。
答案 0 :(得分:2)
有人建议我。要使用XLSX,还需要一个库:
<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
整个pom看起来像这样:
...<dependencies>
<!-- slf4j + log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<!-- XLSTranformer - vyplnovani xlsx sablon -->
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.4.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jxls/jxls-jexcel -->
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jxls/jxls-reader -->
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-jexl -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
用于生成的代码:
公共类ExcelBuilder { 私有静态最终Logger logger = LoggerFactory.getLogger(ExcelBuilder.class);
public static OutputStream generate(String template, Map<String, String> queryParams, String recordName,
List<?> records) {
try (InputStream templateInputStream = ExcelBuilder.class.getResourceAsStream(template)) {
// byte[] result = IOUtils.toByteArray(templateIs);
try (OutputStream outputStream = new FileOutputStream("protokol.xlsx")) {
Context context = new Context();
Entry<String, String> entry = queryParams.entrySet().iterator().next();
// context.putVar(entry.getKey(), entry.getValue());
context.putVar(recordName, records);
JxlsHelper instance = JxlsHelper.getInstance();
instance.processTemplate(templateInputStream, outputStream, context);
return outputStream;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
}
模板基于模板注释标记,如下所述:Jxls Object Collection Sample