2016-05-16 68 views
3

我有一个带有表格的Word文档,它看起来像一个表格。我有像%firstName%, %lastName%, %birthdate%等占位符。 当我使用replace()函数时,%firstName%, %lastName%, %birthdate%和所有其他占位符字段被替换为第一页和第二页。第二秒后,没有任何东西会被替换。第3页和第4页上占位符的所有名称与第1页和第2页相同。我甚至复制并粘贴了占位符名称,并确保没有添加空格。很想知道是否有其他人发生过这种情况,并且可以告诉我为解决问题所做的工作。Coldfusion替换()不适用于MS-Word的所有页面文档

<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> 
+3

与你的问题没有关系,但不需要cflock。 – Leigh

回答

6

有四分之一(可选)参数the replace() method;范围

范围:

之一:替换第一次出现(默认)
所有:替换所有出现

注意,“一”是默认的,并且只替换第一次出现。尝试添加第四个参数是这样的:

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

(哈希标签#没有必要在此位的代码。)

另外要注意的是,replace()方法使用的是区分大小写。

+0

谢谢Miguel-F,就是这样。谢谢一堆! – malibu65k