2013-10-23 61 views
1

我已经创建了Asp.net MVC Project。其中我需要上传.docx文件,并且必须用用户当前的细节和一些动态值替换文本。我已经完成了任务与AssemblyFormat.OpenXml.dll的帮助。 问题是它正在取代一些文本,但一些not.My代码是像这个 -如何替换c#中的.docx文件中的文本

if (document.DocumentName.Contains(".doc") || document.DocumentName.Contains(".docx")) 
       { 
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(File, true)) 
        { 
         using (StreamReader reader = new StreamReader(wordDoc.MainDocumentPart.GetStream())) 
         { 
          documentText = reader.ReadToEnd(); 
         } 
         documentText = documentText.Replace("##Username##", user.Username); 
         documentText = documentText.Replace("##Email##", user.Email); 
         documentText = documentText.Replace("##TSF Number##", convert(NewAccount_210.TSFILE_NUMBER).ToString());//check 
         documentText = documentText.Replace("##A3PThirdPartyId##", convert(NewAccount_210.A3P_THIRD_PARTY_ID)); 
         documentText = documentText.Replace("##Location Code##", convert(NewAccount_210.LOCATION_CODE)); 
         documentText = documentText.Replace("##AccountNumber##", convert(NewAccount_210.ACCT_NUMBER));//check 
         documentText = documentText.Replace("##Transaction Number##", convert(NewAccount_210.TRANSACTION_NUMBER)); 
         documentText = documentText.Replace("##Current Date##", convert(NewAccount_210.CURRENT_DATE).ToString()); 
         documentText = documentText.Replace("##Customer Name##", convert(NewAccount_210.CUSTOMER_NAME)); 
         documentText = documentText.Replace("##SocialSecuirityNumber##", convert(NewAccount_210.SOCIAL_SECURITY_NUM));//check 
         documentText = documentText.Replace("##A3PAmountPlaced##", (convert(NewAccount_210.A3P_AMT_PLACED)).ToString()); 
         documentText = documentText.Replace("##Interest Rate##", (convert(NewAccount_210.INTEREST_RATE)).ToString());//check 

    using (StreamWriter writer = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) 
         { 
          writer.Write(documentText); 
         } 
        } 
} 

在上面的文字像##账户号码##,##当前日期##等一些价值不更换。任何帮助将不胜感激。

+1

这可以帮助你吗? http://netword.codeplex.com/ – Izikon

+0

如果一些元素被替换而另一些元素不被替换,也许这是一个套管问题...... – Gonzix

回答