首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ApachePOI3.10最后的XWPFDocument.setParagraph()

ApachePOI3.10最后的XWPFDocument.setParagraph()
EN

Stack Overflow用户
提问于 2014-08-14 06:12:56
回答 1查看 1.5K关注 0票数 2

我正在查看Apache文档,这两个文档都是随我下载的,也包括在现场的文档。在setParagraph()类中应该有一个org.apache.poi.xwpf.usermodel.XWPFDocument方法。我在ColdFusion中使用这个类,并且无法访问文档中列出的许多方法。

这是由于不正确地使用类或类依赖关系造成的吗?还是由于Apache .jars正在更新而不再支持setParagraph()方法?

更新:

我使用ColdFusion 9并使用JavaLoader.cfc加载类。下面是用于实例化操作所需的类的代码。

代码语言:javascript
复制
<cfset javaLoader = server[application.myJavaLoaderKey]>
<cfset OPCPackage = javaLoader.create("org.apache.poi.openxml4j.opc.OPCPackage")>
<cfset dot_template_opc = OPCPackage.open("pathToDocxFileInstantiated")>
<cfset XWPFDocument = javaLoader.create("org.apache.poi.xwpf.usermodel.XWPFDocument")>
<cfset dot_template = XWPFDocument.init(dot_template_opc)>
<cfset XWPFParagraph = javaLoader.create("org.apache.poi.xwpf.usermodel.XWPFParagraph")>
<cfset XWPFTable = javaLoader.create("org.apache.poi.xwpf.usermodel.XWPFTable")>
<cfset XWPFRun = javaLoader.create("org.apache.poi.xwpf.usermodel.XWPFRun")>
<cfset CTP = javaLoader.create("org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP")>
<cfset CTRImpl = javaLoader.create("org.openxmlformats.schemas.wordprocessingml.x2006.main.impl.CTRImpl")>
<cfset CTText = javaLoader.create("org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText")>
<cfset dot_output = "pathToNewDocxFile"><!--- Edited version of original --->
<cfset outputObject_dot = CreateObject("java","java.io.FiltOutputStream",
        "java",
        "java.io.FileOutputStream"
        ).Init(
            CreateObject(
                "java",
                "java.io.File"
                ).Init(
                    dot_output
                )>

以下是Application.cfc中与JavaLoader.cfc相关的代码。它被保存在onApplicationStart()方法中。

代码语言:javascript
复制
<cfset var myJavaLoaderKey = "ABCDEF-01234567-9876543210-ABCDEF54321_javaloader">
<cfset var jarPaths = arrayNew(1)>

<!--- if the javaLoader was not created yet --->
<cfif NOT structKeyExists(server, myJavaLoaderKey)>

<!--- these are absolute paths to the POI jar files --->
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-examples-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-excelant-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-ooxml-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-ooxml-schemas-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/poi-scratchpad-3.10-FINAL-20140208.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/lib/commons-codec-1.5.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/lib/commons-logging-1.1.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/lib/junit-4.11.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/lib/log4j-1.2.13.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/ooxml-lib/dom4j-1.6.1.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/ooxml-lib/stax-api-1.0.1.jar")) >
<cfset arrayAppend( jarPaths, expandPath("../wwwroot/poi/ooxml-lib/xmlbeans-2.3.0.jar")) >
<cfif NOT structKeyExists(server, myJavaLoaderKey)>
   <cflock name="#Hash(myJavaLoaderKey)#" type="exclusive" timeout="10">
       <!---  create an instance of the JavaLoader and store it in the server scope --->
       <cfset server[myJavaLoaderKey] = createObject("component", "javaloader.JavaLoader").init(loatPaths=jarPaths,loadColdFusionClassPath=true)>
   </cflock>
</cfif>
<cfset application.myJavaLoaderKey = myJavaLoaderKey>
</cfif>

<cfscript>
_Thread = createObject("java", "java.lang.Thread");
currentClassloader = _Thread.currentThread().getContextClassLoader();
try { // Set the current thread's context class loader as Javaloader's classloader, so dom4j doesn't die _Thread.currentThread().setContextClassLoader(server[var.JLKey].getURLClassLoader());
}
catch(Any exc) {
    rethrow;
}
finally { // We have to reset the classloader, due to thread pooling.
    _Thread.currentThread().setContextClassLoader(currentClassloader);
}
</cfscript>

尽管如此,我还没有删除与CF一起打包的原始.jars。我最近尝试过,但没有成功,当然,如果我再努力一点,我相信我可以做到,但是,我觉得这不应该是必要的。

我正在调用CreateObject来实例化java.io.File和java.io.FileOutputStream。这是否以某种方式将我的XWPFDocument的实例化恢复为CF的默认类?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-14 13:24:18

这是由于Apache .jars正在更新,不再支持setParagraph()方法吗?

你能澄清“无法访问”吗?什么是确切的异常和堆栈跟踪?

情况很可能正好相反。实际上,该方法并没有被废弃或删除,而是添加到了比您使用的版本更新的版本中。正如@Gagravarr所提到的,CF提供了POI的旧版本。您没有告诉我们您的CF版本,但是CF10与POI "3.6-beta1“捆绑在一起。活API通常指向最新的稳定版本,目前为"3.10最终版本“。显然,3.10可能包括在使用早期版本时不可用的新方法和/或类。

使用较新版本的POI有几个选项。

  • 用更新的版本覆盖{cf_root}\lib中现有的POI。然后重新启动CF服务器(必需)。注意:我没有亲自这么做,所以我不知道这样做是否会破坏其他功能。
  • 对于CF9或更早版本,请使用JavaLoader.cfc并行运行较新的版本。
  • 到了CF10,JavaLoader.cfc就有了裂痕。因此,您可以在您的this.javaSettings中使用Application.cfc加载POI的更新版本。

附带注意,Word API不如Excel的成熟,所以请记住这一点。

更新1:

如何确定对象“没有setParagraph”方法?如果您执行了一个cfdump,并且没有看到列出的setParagraph()方法,那么您可能没有加载这个类的更新版本。请您发布代码,展示如何实例化JavaLoader和创建XWPFDocument对象?

在不知道更多信息的情况下,我猜你可能是不小心在某个地方使用了createObject()。因此,您仍然得到了这个类的旧版本,而不是更新的版本。ie:

代码语言:javascript
复制
 //this loads the older version in cf_root/lib
 createObject("java", "path.to.Class"); 

 // this loads the newer version in JavaLoader paths
 javaLoader.create("path.to.Class");

更新2:

有意思的。代码正在加载旧版本,但不是出于上述原因。(见调试技巧:标识在ColdFusion MX中从哪个Jar文件加载了类)

我认为造成这个问题的原因是JavaLoder paths参数拼写错误:loatPaths而不是loadPaths。因此,它默认为空数组。其结果是您实际上没有加载任何新的jars。因为loadColdFusionClassPath=true最终会加载旧版本。

附带注意,在CF9中生成路径列表的一种更简单的方法是使用DirectoryList()。该函数将允许您一次抓取所有的路径。这就是说,一个非常重要的注意事项。POI使用dom4j库,它以类加载问题而闻名。加载jar的两个版本将导致问题--即使使用JavaLoader。因此,请确保首先删除/poi/ooxml-lib/dom4j-1.6.1.jar文件。(这也应该消除了交换currentClassloader的需要)。

代码语言:javascript
复制
  jarPaths = DirectoryList(expandPath("/poi/"), true, "path", "*.jar")

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25301195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档