-1

` System.IO.File.Copy(wordFullPath, wordFullPathCopy, true);

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(wordFullPathCopy, true))
        {
            string docText = null;
            using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
                docText = docText.Replace("[$1-1-1$]", "TEST1");
                docText = docText.Replace("[$1-1-2$]", "TEST2");

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

        byte[] byteArray = File.ReadAllBytes(wordFullPathCopy);
        System.IO.File.Delete(wordFullPathCopy);

        Page.Response.Clear();
        Page.Response.AppendHeader("Content-Disposition", "attachment; filename=DocxDllTest.docx");
        Page.Response.ContentType = "application/octet-stream";
        Page.Response.BinaryWrite(byteArray);
        Page.Response.OutputStream.Flush();
        Page.Response.OutputStream.Close();
        Page.Response.Flush();
        Page.Response.End();`

Hi~I have a docX template(Of course,It must read only!!). Now I make a copy and use WordprocessingDocument to replace text.

It worked fine.

But now,I want to not make a copy.

Just replace text and save to memorystream then Response to user.

Is it possible? and How??

Thx!!

Jamie
  • 21
  • 4
  • Does this answer your question? [How to create Excel file using OpenXML without creating a local file?](https://stackoverflow.com/questions/29887503/how-to-create-excel-file-using-openxml-without-creating-a-local-file) – petelids Nov 06 '19 at 12:23
  • I know how to read docX to memorystream , but how to change the text in memorystream ?? – Jamie Nov 07 '19 at 02:20

1 Answers1

0
        byte[] wordByteArray = null;
        string wordFullPath = FileHelper.FileFromFileDir("ATT1.docx");  //Template
        wordByteArray = File.ReadAllBytes(wordFullPath);

        using (MemoryStream ms = new MemoryStream())
        {
            string docText = null;
            ms.Write(wordByteArray, 0, (int)wordByteArray.Length);
            using (WordprocessingDocument doc = WordprocessingDocument.Open(ms, true))
            {
                using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
                {
                    docText = sr.ReadToEnd();
                    docText = docText.Replace("[$1-1-1$]", "TEST1");
                    docText = docText.Replace("[$1-1-2$]", "TEST2");
                }

                byte[] docTextByteArray = Encoding.UTF8.GetBytes(docText);
                MemoryStream memDocText = new MemoryStream(docTextByteArray);
                doc.MainDocumentPart.FeedData(memDocText);
            }

            Page.Response.Clear();
            Page.Response.AppendHeader("Content-Disposition", "attachment; filename=DocxDllTest.docx");
            Page.Response.ContentType = "application/octet-stream";
            Page.Response.BinaryWrite(ms.ToArray());
            Page.Response.OutputStream.Flush();
            Page.Response.OutputStream.Close();
            Page.Response.Flush();
            Page.Response.End();

OK!!It work!! Thank stackOverflow's users!!

Jamie
  • 21
  • 4
  • Replacing text in an Open XML string like in your example is not safe and might not produce the desired result! While you are not going to produce invalid XML in your specific example (noting that other replacements could do that), this approach might not work if you edit your template using Microsoft Word. Editing the document might turn a single run `[$1-1-1$]` into two runs `[$1-1-1$]` (still representing the same text `"[$1-1-1$]"`), in which case you will not be able to replace `"[$1-1-1$]"` with `"TEST1"`. – Thomas Barnekow Dec 06 '19 at 19:29