Ответить новое слово из шаблона без копии - PullRequest
0 голосов
/ 06 ноября 2019

`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();`

Привет ~ У меня есть шаблон docX (конечно, он должен только для чтения !!). Теперь я делаю копию и использую WordprocessingDocument для замены текста.

Все работало нормально.

Но теперь я не хочу делать копию.

Просто замените текст и сохранитек потоку памяти, то Ответ пользователю.

Возможно ли это? и как ??

Thx !!

1 Ответ

0 голосов
/ 07 ноября 2019
        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();

ОК !! Это работает !! Спасибо пользователям stackOverflow !!

...