ColdFusion的XML支持和Word文档



使用ColdFusion的新XML函数。。。是否可以在Word文档(docx)中查找/替换文本?我正试图解决这个问题,因为一个docx文档可能有一段文本(一个要搜索的占位符)分布在多个w:r(运行)上,这可能很难搜索和替换。

有可能吗?对

然而,MS使用的XML模式非常冗长。我知道这是多余的,但XML相当复杂。

最好使用.NET来操作文档,并使用.NET集成从CF内部进行调用。我相信也有Java库,但我不确定它们有多容易使用或工作得有多好。

我所做的是用一个替换词替换单词document,然后使用7zip的CLI提取.docx文件,因为它只是一个存档,替换单词,然后重新排列它。这已经被证明是非常有效的,而且根本不会干扰XML。

以下是应用程序中的一些示例代码,它替换了单词"%SENTDATE%"。并不是所有的变量都在这里,但这应该足以让你开始。

<cfset EditFile = Output & "worddocument.xml" />
<!--- Extract the New Document --->
<cfexecute name="7za.exe" arguments='x -y "#StartingFile#" -o"#Output#"' outputfile="#Dir#log_Extract.txt" />
<!--- Read in the file --->
<cfset WordDoc = FileRead( EditFile ) />
<!--- Replace Values --->
<cfset WordDoc = Replace( WordDoc, "%SENTDATE%", DateFormat( Now(), "MMMM DD, YYYY" ), "ALL" ) />
<!--- Save File --->
<cfset FileWrite( EditFile, WordDoc ) />
<!--- Delete Archive if it exists --->
<cfif FileExists( NewFile )>
    <cfset FileDelete( NewFile ) />
</cfif>
<!--- Repack Archive --->
<cfexecute name="7za.exe" arguments='a "#NewFile#" "#Output#*"' outputfile="#Dir#log_Archive.txt" />
<!--- Rename Archive --->
<cffile action="rename" source="#NewFile#" destination="#ListFirst( NewFile, "." )#.docx" />

最新更新