Note that there are some explanatory texts on larger screens.

plurals
  1. POOpen xml replace text from word file and return memory stream using MVC
    primarykey
    data
    text
    <p>I have an word file that contain my specified pattern text {pattern} and I want to replace those pattern with new my string which was read from database. So I used open xml read stream from my docx template file the replace my pattern string then returned to stream which support to download file without create a temporary file. But when I opened it generated me error on docx file. Below is my example code</p> <pre><code>public ActionResult SearchAndReplace(string FilePath) { MemoryStream mem = new MemoryStream(System.IO.File.ReadAllBytes(FilePath)); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true)) { string docText = null; using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream())) { docText = sr.ReadToEnd(); } Regex regexText = new Regex("Hello world!"); docText = regexText.Replace(docText, "Hi Everyone!"); //Instead using this code below to write text back the original file. I write new string back to memory stream and return to a stream download file //using (StreamWriter sw = new //StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create))) //{ // sw.Write(docText); //} using (StreamWriter sw = new StreamWriter(mem)) { sw.Write(docText); } } mem.Seek(0, SeekOrigin.Begin); return File(mem, "application/octet-stream","download.docx"); //Return to download file } </code></pre> <p>Please suggest me any solutions instead read a text from a word file and replace those expected pattern text then write data back to the original file. Are there any solutions replace text with WordprocessingDocument libary? How can I return to memory stream with validation docx file format?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload