我有一个带有正确结构的文字生成的xml文件(2007)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
<!-- content... -->
</pkg:package>
使用Apache Poi,我想生成相应的XWPFDocument
try {
//strXML is the xml content
File xmlGenFile = new File("./sampleFile.xml");
FileWriter fw = new FileWriter(xmlGenFile);
fw.write(strXML);
fw.close();
OPCPackage oPack = OPCPackage.open(xmlGenFile);
XWPFDocument doc = new XWPFDocument(oPack);
} catch (ContentIOException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我尝试使用OPCPackage.open
方法但我得到了org.apache.poi.openxml4j.exceptions.InvalidOperationException
org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: '.\sampleFile.xml'
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:131)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:246)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:200)
at tn.com.st2i.service.impl.TemplatingServiceImpl.generateWordDocument(TemplatingServiceImpl.java:105)
at tn.com.st2i.webscript.RetrieveGeneratedDocumentPost.execute(RetrieveGeneratedDocumentPost.java:79)
at org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:429)
at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:452)
at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:491)
at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:529)
at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:341)
at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:378)
at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:215)
at java.util.zip.ZipFile.<init>(ZipFile.java:145)
at java.util.zip.ZipFile.<init>(ZipFile.java:159)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:157)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:129)
... 32 more
显然,该文件存在
答案 0 :(得分:0)
从堆栈跟踪中可以清楚地看出,您提供的是一个裸XML文件。一个真正的Word 2007文档由ZIP和ZIP存档中的其他文件组成。
答案 1 :(得分:0)
你有什么是Flat OPC XML。
我不知道POI是否可以处理,但docx4j(我工作)可以。
请参阅示例代码ConvertInFlatOpenPackage.java
顺便说一句,在Word文档本身中也有各种处理“变量”的选项(即与Flat OPC表示相对);最好的恕我直言是内容控制数据绑定。