Coldfusion Replace() 不适用于 MS-Word 文档的所有页面

Coldfusion Replace() not working on all pages for MS-Word Document

我有一个 word 文档,其中的表格看起来像表格。我有 %firstName%, %lastName%, %birthdate%...等占位符。 当我使用 replace() 函数时,第一页和第二页上的 %firstName%, %lastName%, %birthdate% 和所有其他占位符字段都被替换了。第二次之后,没有什么可以替代。第3页和第4页的所有占位符名称与第1页和第2页相同。我什至复制并粘贴了占位符名称,并确保没有添加空格。很想知道是否有其他人遇到过这种情况,并且可以告诉我如何解决它。

<cfset docPath =  GetDirectoryFromPath(GetCurrentTemplatePath()) & "UserTemplate.rtf" />        
<cflock name="UserTemp" type="exclusive" timeout="30">
    <cfset rtf = FileRead(docPath) />
    <cfquery name = "qUserFormData">
        SELECT * FROM vUserFormData WHERE UserID = 3
    </cfquery>
    <cfset rtf = Replace(rtf,"%firstName%",#firstName#)/>
    <cfset rtf = Replace(rtf,"%lastName%",#lastName#) />
    <cfset rtf = Replace(rtf,"%birthday%",#birthday#) />
</cflock>
<cfheader name="content-disposition" value="filename=UserTemplate.doc" />
<cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>

the replace() method; scope 有第四个(可选)参数。

Scope:

one: replaces the first occurrence (default)
all: replaces all occurrences

请注意,"one" 是默认值,它只会替换第一次出现的值。尝试像这样添加第四个参数:

<cfset rtf = Replace(rtf,"%firstName%",firstName,"all") />
<cfset rtf = Replace(rtf,"%lastName%",lastName,"all") />
<cfset rtf = Replace(rtf,"%birthday%",birthday,"all") />

(散列标签 # 在这段代码中不是必需的。)

另请注意,您使用的 replace() 方法区分大小写。