Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Open XML - Create Document, Add Text to it, and Output using MemoryStream

$
0
0
I'm using the code below which creates a document and some sample text, except the sample text does not appear when I open the document.

Can anyone help me out here?

Thanks

    protected void cmdGenerate_Click(object sender, EventArgs e)
    {
        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();

                new Document(new Body()).Save(mainPart);

                // Assign a reference to the existing document body.
                Body body = mainPart.Document.Body;

                // Add new text.
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text("this is sample text"));

                mainPart.Document.Save();
                // Stream it down to the browser
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + txtManufacturer.Text + ".docx");
                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                mem.CopyTo(Response.OutputStream);
                wordDocument.Close();
                Response.End();
            }

        }
}




Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>