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