我收到错误:: org.apache.poi.openxml4j.exceptions.InvalidFormatException无法解析。
这里是我的java类的代码...我想要的是生成一个excel报告..请告诉我如何在transformer.transformXLS(templateFileName,beans,destFileName)方法中给出路径..我应该复制excel文件在我的web项目中的某个地方?
我使用takeinput.jsp将输入作为来自user ::的id,然后id的值将转到ReportServlet然后转到ReportGen ...我正在编写jdbc代码以从数据库中获取值。并生成excel表。请帮忙 ??
takeinput.jsp
<body>
<form id="reportformid" name="reportform" action="ReportServlet" method="get">
Enter the ID of the person :: <input id="idid" type="text" name="id"/><br><br><br>
<button id="button" name="reportbutton" type="submit"></button>
</form>
</body>
web.xml ::
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>Name</servlet-name>
<servlet-class>
classes.web.ReportServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Name</servlet-name>
<url-pattern>/ReportServlet</url-pattern>
</servlet-mapping>
</web-app>
ReportServlet.java(仅限get方法)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
ReportGen obj = new ReportGen();
obj.getReport(id);
}
ReportGen.java
package classes.web;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.RowSetDynaClass;
import net.sf.jxls.report.ResultSetCollection;
import net.sf.jxls.transformer.XLSTransformer;
public class ReportGen {
HandleConnections obj = new HandleConnections();
Connection con=null;
ResultSet rs = null;
Statement st = null;
public void getReport(String id){
String templateFileName = "/GenerateReport/WebContent/WEB-INF/lib/Source.xls";
String destFileName = "/GenerateReport/WebContent/WEB-INF/lib/Dest.xls";
try{
con = obj.getConnection();
st = con.createStatement();
String query = "SELECT F_Name, L_Name, P_Address FROM Employee_Table";
rs = st.executeQuery(query);
RowSetDynaClass rsdc = new RowSetDynaClass(rs, false);
Map beans = new HashMap();
beans.put( "employee", rsdc.getRows() );
XLSTransformer transformer = new XLSTransformer();
transformer.transformXLS( templateFileName, beans, destFileName);
} throws ParsePropertyException
catch(Exception e){System.out.println(e);}
}
}
Stacktrace ::
Feb 22, 2012 3:09:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Name] in context with path [/GenerateReport] threw exception [Servlet execution threw an exception] with root cause
java.lang.Error: Unresolved compilation problems:
The type org.apache.poi.openxml4j.exceptions.InvalidFormatException cannot be resolved. It is indirectly referenced from required .class files
The type org.apache.poi.ss.usermodel.Workbook cannot be resolved. It is indirectly referenced from required .class files
The method transformXLS(String, Map, String) from the type XLSTransformer refers to the missing type InvalidFormatException
Syntax error on tokens, delete these tokens
at classes.web.ReportGen.<init>(ReportGen.java:32)
at classes.web.ReportServlet.doGet(ReportServlet.java:30)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:2)
您的类路径中可能错过了Apache POI jar。
答案 1 :(得分:0)
如果仔细观察,这是一个没有必要的JAR文件的问题。你的代码引用了库(一个jar文件),它依赖于一个类(InvalidFormatException),它不是那个jar的一部分,必须在其他一些JAR文件中。从我可以通过一些谷歌搜索来看,这是在apache poi包中,其JAR文件不在你的类路径上。
答案 2 :(得分:0)
jxls-core.jar具有以下依赖关系树:
[INFO] \- net.sf.jxls:jxls-core:jar:1.0.3:compile
[INFO] +- org.apache.commons:commons-jexl:jar:2.0.1:compile
[INFO] \- org.apache.poi:poi-ooxml:jar:3.9:compile
[INFO] \- org.apache.poi:poi-ooxml-schemas:jar:3.9:compile
[INFO] \- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
[INFO] \- stax:stax-api:jar:1.0.1:compile
您需要将所有这些内容放在类路径中。
更具体地说,'org.apache.poi.openxml4j.exceptions.InvalidFormatException'类位于'poi-ooxml-schemas.jar'中。
这就是我喜欢使用maven的原因。 maven会自动解决所有这些问题。