将.docx转换为ooxml文档包的代码

时间:2014-11-24 16:59:24

标签: c# .net openxml docx

我能够使用OpenXML获取docx文件的MainDocumentPart,但无法获得如下所示的ooxml文档包。任何人都能对此有所了解吗?

<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
  <pkg:part pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">
    <pkg:xmlData>
      <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
        <Relationship Id="rId2" Type="http://schemas.microsoft.com/office/2011/relationships/webextensiontaskpanes" Target="word/webextensions/taskpanes.xml"/>
        <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
      </Relationships>
    </pkg:xmlData>
  </pkg:part>

1 个答案:

答案 0 :(得分:2)

你所拥有的是“Flat OPC”XML。

您可以使用Eric White's approach

或者您可以使用docx4j.NET。为此,首先将其转换为docx4j WordprocessingMLPackage。如果您从C#字符串开始:

    private WordprocessingMLPackage getPkgFromString(string wordOpenXML)
    {

        // The string is UTF-16; convert it to UTF-8
        byte[] utf16Bytes = Encoding.Unicode.GetBytes(wordOpenXML);
        byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);

        return  WordprocessingMLPackageFactory.createWordprocessingMLPackage(utf8Bytes);
    }

然后要获得C#WordprocessingDocument,请使用WordprocessingDocumentFactory.cs