【问题标题】:Creating a word document (docx) with OpenXml使用 OpenXml 创建 Word 文档 (docx)
【发布时间】:2017-09-05 06:50:03
【问题描述】:

首先让我说我已经阅读了其他类似的问题,但解决方案(复制如下)对我不起作用。

我正在尝试使用 .net 核心和 OpenXMl(使用 DocumentFormat.OpenXml 2.7.2 nuget 包)创建一个 word 文档(docx)。 看起来微不足道,但不知何故它不起作用。当我尝试打开文档时,我收到文件已损坏、被截断或格式不正确的错误消息。

这是我的代码(我在许多教程中都找到了):

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;

public Stream GetDocument()
        {
            var stream = new MemoryStream();

            using (WordprocessingDocument doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true))
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();

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

                Body body = mainPart.Document.Body;
                body.Append(new Paragraph(
                            new Run(
                                new Text("Hello World!"))));

                mainPart.Document.Save();

            }
            stream.Seek(0, SeekOrigin.Begin);

            return stream;

        }

广告保存如下:

 public static void Test()
        {
            DocxWriter writer = new DocxWriter();

            string filepath = Directory.GetCurrentDirectory() + @"/test.docx";

            var stream = writer.GetDocument();

            using (var fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write))
            {
                stream.CopyTo(fileStream);
            }

            stream.Dispose();
        }

编辑: 提取 docx 后,我可以找到如下所示的底层 xml:

<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:p>
            <w:r>
                <w:t>Hello World!</w:t>
            </w:r>
        </w:p>
    </w:body>
</w:document>

【问题讨论】:

  • 您使用哪个版本的office来打开.docx文件?
  • @Niladri 实际上,我是在没有办公室的情况下从 Mac 打开它,但打开其他 docx 文件没有问题。我已经要求其他人尝试在 Windows 上打开它,它显示出类似的问题。这就是为什么我认为这不是读者的问题。
  • 我刚刚验证了它可以正常工作。在 Win10 上构建、运行和生成文件。并在 Word2016 中打开文件。您能否发布有关您的项目的更多详细信息,因为到目前为止所有这些都运行良好。
  • 我还将文件通过电子邮件发送到我的 mac,并在 Word for mac 上打开。

标签: c# .net-core openxml docx openxml-sdk


【解决方案1】:

所以我的解决方案看起来像这样。我有几个全局变量:

private MemoryStream _Ms;
private WordprocessingDocument _Wpd;

那么创建方法是这样的:

public Doc()
{
    _Ms = new MemoryStream();
    _Wpd = WordprocessingDocument.Create(_Ms, WordprocessingDocumentType.Document, true);
    _Wpd.AddMainDocumentPart();
    _Wpd.MainDocumentPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();
    _Wpd.MainDocumentPart.Document.Body = new Body();
    _Wpd.MainDocumentPart.Document.Save();
    _Wpd.Package.Flush();
}

保存方法是这样的:

public void SaveToFile(string fullFileName)
{
    _Wpd.MainDocumentPart.Document.Save();

    _Wpd.Package.Flush();

    _Ms.Position = 0;
    var buf = new byte[_Ms.Length];
    _Ms.Read(buf, 0, buf.Length);

    using (FileStream fs = new System.IO.FileStream(fullFileName, System.IO.FileMode.Create))
    {
        fs.Write(buf, 0, buf.Length);
    }
}

而且效果很好。试试这个。

【讨论】:

  • 谢谢,但是当我尝试打开它时它给了我同样的错误:(
  • 抱歉这么晚才回复。由于您的努力,我对您的答案投了赞成票,但它仍然对我不起作用。我想跨平台部门存在一些问题。我注意到其他一些作者(如 onedrive.live.com、evidenceprime.github.io/html-docx-js/test/sample.html 等)的这种行为。它们都生成在 Mac 上无法使用的 docx 文件。更奇怪的是,docx在Mac上可以打开,在Windows下打不开。
【解决方案2】:

对于遇到此问题的其他人 - 这是 open-xml-sdk 中的一个错误,在此处报告: https://github.com/OfficeDev/Open-XML-SDK/issues/249

看起来 _rels/.rels 隐藏文件的路径存在问题,添加了一个额外的反斜杠,导致 mac 出现问题。

我当前的修复/破解是使用现有的空文档作为模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多