相关疑难解决方法(0)

使用Open XML创建word文档

我正在创建一个示例处理程序来生成简单的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 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net ms-word openxml openxml-sdk

10
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net ×1

c# ×1

ms-word ×1

openxml ×1

openxml-sdk ×1