Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

c# - create word document with Open XML

I am creating a sample handler to generate simple Word document.
This document will contains the text Hello world

This is the code I use (C# .NET 3.5),
I got the Word document created but there is no text in it, the size is 0.
How can I fix it?
(I use CopyStream method because CopyTo is available in .NET 4.0 and above only.)

public class HandlerCreateDocx : IHttpHandler
{
    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();
                // Stream it down to the browser
                context.Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
                context.Response.ContentType = "application/vnd.ms-word.document";
                CopyStream(mem, context.Response.OutputStream);
                context.Response.End();
            }
        }
    }

    // Only useful before .NET 4
    public void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
        int bytesRead;

        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...