create word document with Open XML

ⅰ亾dé卋堺 提交于 2019-11-29 02:04:45
Tuyen Nguyen

This works for me, by putting the streaming code in the outer USING block.

This causes a call to WordprocessingDocument.Close (via the Dispose method).

public void ProcessRequest(HttpContext context)
{
    using (MemoryStream mem = new MemoryStream())
    {
        // Create Document
        using (WordprocessingDocument wordDocument =
            WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
        {
            // Add a main document part. 
            MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

            // Create the document structure and add some text.
            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            run.AppendChild(new Text("Hello world!"));
            mainPart.Document.Save();
        }

        context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        context.Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
        mem.Seek(0, SeekOrigin.Begin);
        mem.CopyTo(context.Response.OutputStream);
        context.Response.Flush();
        context.Response.End();
    }
}
Toan Vo

I have modifed your code to make it work. I can open it correctly after save download it. Please see my modified below. Hope this help.

using (MemoryStream documentStream = new MemoryStream())
{
    using (WordprocessingDocument myDoc = WordprocessingDocument.Create(documentStream, WordprocessingDocumentType.Document, true))
    {
        // Add a new main document part. 
        MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
        //Create Document tree for simple document. 
        mainPart.Document = new Document();
        //Create Body (this element contains
        //other elements that we want to include 
        Body body = new Body();
        //Create paragraph 
        Paragraph paragraph = new Paragraph();
        Run run_paragraph = new Run();
        // we want to put that text into the output document 
        Text text_paragraph = new Text("Hello World!");
        //Append elements appropriately. 
        run_paragraph.Append(text_paragraph);
        paragraph.Append(run_paragraph);
        body.Append(paragraph);
        mainPart.Document.Append(body);

        // Save changes to the main document part. 
        mainPart.Document.Save();
        myDoc.Close();
        context.Response.ClearContent();
        context.Response.ClearHeaders();
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        SetContentType(context.Request, context.Response, "Simple.docx");
        documentStream.Seek(0, SeekOrigin.Begin);
        documentStream.CopyTo(context.Response.OutputStream);
        context.Response.Flush();
        context.Response.End();
    }
}
Zoyeb Shaikh
string Filepath = @"C:\Users\infinity\Desktop\zoyeb.docx";
using (var wordprocessingDocument = WordprocessingDocument.Create(Filepath, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = wordprocessingDocument.AddMainDocumentPart();
    mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
    Body body = mainPart.Document.AppendChild(new Body());
    DocumentFormat.OpenXml.Wordprocessing.Paragraph para = body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph());
    DocumentFormat.OpenXml.Wordprocessing.Run run = para.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Run());
    run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text("siddiq"));
    wordprocessingDocument.MainDocumentPart.Document.Save();
}

go to nuget package manager and install this first into your project

Install-Package DocumentFormat.OpenXml -Version 2.8.1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!