我有创建 Asp.net MVC 项目。在其中我需要上传一个 .docx 文件,并且必须用用户当前的详细信息和一些动态值替换文本。我在 Assembly DocumentFormat.OpenXml.dll 的帮助下完成了这项任务。问题是它正在替换一些文本但有些不是。我的代码是这样的 -
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);
}
}
}
在上面的文本中,##AccountNumber##、##Current Date## 等一些值没有被替换。任何帮助都将不胜感激。