我需要做docx操作(在占位符上查找/替换以及检查/取消复选框)。由于ColdFusion 10与Java集成得很好,所以我决定尝试使用Java docx4j,它基本上是模仿Java (.net平台)的。
我将docx4j JAR放在一个自定义文件夹中,我已经通过JavaSettings在Application.cfc中安装了这个JAR(在CF10中是新的,我在其他JAR中试用了它,它可以工作):
<cfcomponent output="false">
<cfset this.javaSettings =
{LoadPaths = ["/myJava/lib"], loadColdFusionClassPath = true, reloadOnChange= true,
watchInterval = 100, watchExtensions = "jar,class,xml"} />
</cfcomponent>现在,我尝试使用以下示例:https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/samples/VariableReplace.java
但是试图调用WordprocessingMLPackage失败了,函数CreateObject()表示不存在特定的类:
<cfset docObj = createObject("java","org.docx4j.openpackaging.packages.WordprocessingMLPackage") />有什么想法吗?我并不是一个真正的Java人,但是没有多少用于docx操作的选项。
发布于 2012-05-31 23:13:44
好的。好像我把一切都搞定了。我只需要弄清楚如何在docx文档中查找/替换,以及我想要做的其他事情。到目前为止,我的代码是向各位展示它看起来工作正常(如果您在Application.cfc上,请确保您的CF10看起来像原来的帖子):
<cfscript>
docPackageObj = createObject("java","org.docx4j.openpackaging.packages.WordprocessingMLPackage").init();
docObj = createObject("java","org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart").init();
xmlUtilObj = createObject("java","org.docx4j.XmlUtils").init();
wmlDocObj = createObject("java","org.docx4j.wml.Document").init();
saveToZipFile = createObject("java","org.docx4j.openpackaging.io.SaveToZipFile").init(docPackageObj);
strFilePath = getDirectoryFromPath(getCurrentTemplatePath()) & "testDoc.docx";
wordMLPackage =
docPackageObj.load(createObject("java","java.io.File").init(javaCast("string",strFilePath)));
documentPart = wordMLPackage.getMainDocumentPart();
// unmarshallFromTemplate requires string input
strXml = xmlUtilObj.marshaltoString(documentPart.getJaxbElement(),true);
writeDump(var="#strXml#");
</cfscript>现在,有谁知道如何将ColdFusion中的结构转换为hashmap(或一般的集合)?我认为CF中的结构实际上是util.Vector,而hashmap是util.HashMap。我在Docx4j中看到的演示在占位符中查找/替换的所有示例如下所示:
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("colour", "green");
mappings.put("icecream", "chocolate");发布于 2012-05-31 11:55:44
您是否尝试过设置loadColdFusionClassPath = false而不是true?也许与运送w/ CF的一些JAR有冲突。
发布于 2012-06-01 05:51:19
(这并不是一个新的答案,但它的注释代码太多了。)
下面是docx4j VariableReplace.java示例的完整代码
<cfscript>
saveToDisk = true;
inputFilePath = ExpandPath("./docx4j/sample-docs/word/unmarshallFromTemplateExample.docx");
outputFilePath = ExpandPath("./OUT_VariableReplace.docx");
inputFile = createObject("java", "java.io.File").init(inputFilePath);
wordMLPackage = createObject("java","org.docx4j.openpackaging.packages.WordprocessingMLPackage").load(inputFile);
documentPart = wordMLPackage.getMainDocumentPart();
XmlUtils = createObject("java","org.docx4j.XmlUtils");
xmlString = XmlUtils.marshaltoString(documentPart.getJaxbElement(),true);
mappings = createObject("java", "java.util.HashMap").init();
mappings["colour"] = "green";
mappings["icecream"] = "chocolate";
obj = XmlUtils.unmarshallFromTemplate(xmlString , mappings);
documentPart.setJaxbElement(obj);
if (saveToDisk) {
saveToZipFile = createObject("java","org.docx4j.openpackaging.io.SaveToZipFile").init(wordMLPackage);
SaveToZipFile.save( outputFilePath );
}
else {
WriteDump(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true, true));
}
</cfscript>https://stackoverflow.com/questions/10825539
复制相似问题