Pregunta

I have Creating Asp.net MVC Project.In which I need to upload a .docx file and have to replace text with user current detail and some dynamic values.I have done this task with help of Assembly DocumentFormat.OpenXml.dll. The Issue is it is replacing some text but some not.My code is something like this-

    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);
                        }
                    }
}

In Above text some values like ##AccountNumber##,##Current Date## etc are not replacing.Any help will be Appreciated.

¿Fue útil?

Solución

Hi your problem is probably not a casing issue. It seems to be the same as that question:

Why Office OpenXML splits text between tags and how to prevent it?

Indeed, the text inside the docx might be splitted:

##Interest Rate##

-> splitted

<w:t>##Interest</w:t>

<w:t> Rate##</w:t>

The replace won't find that string (have a look at the link)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top