早上好, 我试图复制这个:http://jxls.sourceforge.net/getting_started.html
使用我自己的集体诉讼代替员工。 但我有一些问题。
这是我的代码:
package src;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.LocalTime;
import java.util.ArrayList;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Excel {
public static void main(String[] args)throws IOException, InterruptedException{
String srcFilePath = "C:/Users/BKLYN/workspace/GenerateABSValues/action_template.xls";
String destFilePath = "C:/Users/BKLYN/workspace/GenerateABSValues/actionCour.xls";
LocalTime time = LocalTime.now();
Logger logger = LoggerFactory.getLogger(Excel.class);
logger.info("Test Action");
//List<action> listAction = generateData(time);
try(InputStream is = Excel.class.getResourceAsStream(srcFilePath)) {
try (OutputStream os = new FileOutputStream(destFilePath)) {
List<action> listAction = generateData(time);
Context context = new Context();
context.putVar("action", listAction);
JxlsHelper.getInstance().processTemplate(is, os, context);
System.out.println("5");
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
}
private static List<action> generateData(LocalTime time){
List<action> listAction = new ArrayList<>();
for (int i =0; i<10; i++){
listAction.add(new action("AB SCIENCE", time, i));
System.out.println("name=" + "AB SCIENCE" + " time= " + time + " Value= " + i);
}
return listAction;
}
}
我生成了操作的值,这是本教程的员工。
我将这些jar文件添加到我的项目中:
asm-3.3.1.jar -
cglib-2.2.2.jar -
commons-beanutils-1.9.2.jar -
commons-codec-1.9.jar -
commons-collections-3.2.1.jar -
commons-digester3-3.2-with-deps.jar -
commons-jexl-2.1.1.jar -
commons-logging-1.1.1.jar -
jcl-over-slf4j-1.7.12.jar -
jxl-2.6.10.jar -
log4j-1.2.14.jar -
logback-classic-1.1.3.jar -
logback-core-1.1.3.jar -
poi-3.12.jar -
poi-ooxml-3.12.jar -
poi-ooxml-schemas-3.12.jar -
slf4j-api-1.7.12.jar -
stax-api-1.0.1.jar -
xmlbeans-2.6.0.jar -
jxls-2.3.0.jar -
jxls-jexcel-1.0.6.jar -
jxls-poi-1.0.9.jar -
jxls-reader-2.0.2.jar -
当我尝试运行该项目时,我得到了这个:
19:38:14.601 [main] DEBUG org.jxls.util.TransformerFactory - Transformer class is org.jxls.transform.poi.PoiTransformer
19:38:14.608 [main] ERROR org.jxls.util.TransformerFactory - Method createTransformer of org.jxls.transform.poi.PoiTransformer class thrown an Exception
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~ [na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:35) ~[jxls-2.3.0.jar:na]
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:202) [jxls-2.3.0.jar:na]
at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:93) [jxls-2.3.0.jar:na]
at src.Excel.main(Excel.java:43) [classes/:na]
Caused by: java.lang.NullPointerException: null
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:162) ~[poi-ooxml-3.12.jar:3.12]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:143) ~[poi-ooxml-3.12.jar:3.12]
at org.jxls.transform.poi.PoiTransformer.createTransformer(PoiTransformer.java:49) ~[jxls-poi-1.0.9.jar:na]
at org.jxls.transform.poi.PoiTransformer.createTransformer(PoiTransformer.java:42) ~[jxls-poi-1.0.9.jar:na]
... 8 common frames omitted
java.lang.IllegalStateException: Cannot load XLS transformer. Please make sure a Transformer implementation is in classpath
at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:204)
at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:93)
at src.Excel.main(Excel.java:43)
我现在拥有的依赖项:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>1.0.6</version>
</dependency>
</dependencies>
我试图在依赖项中添加许多不同的东西,没有任何改变。我用maven&gt;更新了项目更新项目。这是最糟糕的。我不同时间重新创建项目以正确添加jar以避免冲突。我知道其他人已经要求这个,但我找不到正确的答案。
我不擅长使用java,所以错误很明显。什么是丢失的文件以及我在哪里/如何添加它?
谢谢。
答案 0 :(得分:1)
如果你使用maven,maven可以在构建时破坏excel文件。
JXLS报告的异常可能不明确,它实际上是在创建一个excel文件例外。
你可以这样做:
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
<excludes>
<exclude>template/*.*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/</directory>
<filtering>false</filtering>
<includes>
<include>template/*.*</include>
</includes>
</resource>
</resources>
答案 1 :(得分:0)
尝试了很多并解决了问题,但添加了以下用于解决此问题的依赖JAR列表
公地编解码器1.10.jar 公地collections4-4.1.jar 公地JEXL-2.1.1.jar 共享记录-1.2.jar JXLS-2.4.0.jar JXLS-jexcel-1.0.6.jar JXLS-POI-1.0.12.jar JXLS阅读器 - 2.0.2.jar POI-3.14.jar POI-OOXML-3.15.jar SLF4J-API-1.6.6.jar