2016-05-16 71 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