OpenXML:读取word文件,添加一些文本并打开它,而不将其保存在ASP.Net Web应用程序中

问题描述 投票:0回答:1

我试图从磁盘读取模板文件并添加一个段落。完成后,我想打开它作为一个新文件。模板不应将更改保存到其中。我尝试了下面的代码,结果是,模板随着更改而被修改,但是在浏览器中打开的文件没有这些更改。很明显,我没有得到修改后的流作为回应。我该怎么做,也避免更改模板文件。

public class DocumentCreator
    {
        public static void CreateDoc()
        {
            string strDoc = @"C:\Ash\foo.docx";
            string txt = "Bruno Rovani";

            using (Stream stream = File.Open(strDoc, FileMode.Open))
            {
                OpenAndAddToWordprocessingStream(stream, txt);
            }
        }

        public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
        {
            // Open a WordProcessingDocument based on a stream.
            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
            {
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
                // Add new text.
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text(txt));

                // HTTP response
                HttpResponse Response = HttpContext.Current.Response;
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");

                //stream = wordprocessingDocument.MainDocumentPart.GetStream();
                stream.Position = 0;
                stream.CopyTo(Response.OutputStream);
                Response.Flush();
                Response.End();

            }
        }
    }
asp.net .net ms-word openxml openxml-sdk
1个回答
0
投票

解决

添加了wordprocessingDocument.MainDocumentPart.Document.Save();

PS:模板仍在修改中。不要以为有办法避免这种情况,但是因为我将在实际场景中进行查找和替换,这对我来说不是一个大问题。

所以功能看起来像

public static void OpenAndAddToWordprocessingStream(Stream stream, string txt)
        {
            // Open a WordProcessingDocument based on a stream.
            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(stream, true))
            {
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
                // Add new text.
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text(txt));
                **wordprocessingDocument.MainDocumentPart.Document.Save();**

                // HTTP response
                HttpResponse Response = HttpContext.Current.Response;
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");

                stream.Seek(0, SeekOrigin.Begin);
                stream.CopyTo(Response.OutputStream);
                Response.Flush();
                Response.End();

            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.