使用Open XML创建word文档
本文关键字:word 文档 创建 XML Open 使用 | 更新日期: 2023-09-27 18:10:54
我正在创建一个示例处理程序来生成简单的Word文档
此文档将包含文本Hello world
这是我使用的代码(C#.NET 3.5(,
我已创建Word文档,但其中没有文本,大小为0。
我该怎么修
(我使用CopyStream方法,因为CopyTo仅在.NET 4.0及更高版本中可用。(
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);
}
}
}
通过将流代码放入外部USING块,这对我来说很有效。
这将导致对WordprocessingDocument.Close
的调用(通过Dispose方法(。
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();
}
}
我已经修改了您的代码以使其工作。保存下载后我可以正确打开它。请看下面我的修改。希望得到帮助。
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();
}
}
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();
}
转到nuget包管理器,并首先将其安装到您的项目中
Install-Package DocumentFormat.OpenXml -Version 2.8.1