sách gpt4 ăn đã đi

c# - Tạo tài liệu Word (docx) bằng cách sử dụng dữ liệu trong tệp XML/Chuyển đổi tài liệu XML sang Word dựa trên mẫu

In lại 作者:数据小太阳 更新时间:2023-10-29 02:00:17 31 4
mua khóa gpt4 giày nike

我有一个 XML 文件,其中包含需要在 Word 文档中填充的数据。

我需要找到一种方法来定义一个模板,该模板可用作从 XML 文件填充数据并创建输出文档的基线。

我相信有两种方法可以做到这一点。

  • 创建一个将作为"template"的 XSLT 文件,并使用它与 XML 文件结合使用它来生成 Word 文档。
  • 使用 Word 中的内容控件创建模板文档并以某种方式映射到 XML 文件。

  • 我只是不知道如何实现这两种方式的细节。或者不确定是否有另一种更简单的方法来完成此任务。

    有人可以举例说明如何实现这一点。一个简单的例子就足够了。

    我更喜欢 C# 进行任何编码。我正在使用 Word 2016,但希望它能够从 Word 2007 到 Word 2016 以及两者之间的所有内容(如果可能)兼容,因为用户将使用这些版本。谢谢!

    câu trả lời hay nhất

    弄清楚如何使用内容控件生成文档以及如何将 XML 中的数据填充到内容控件中。我把它分为两部分:

  • 第 1 部分:为文档生成创建模板文档
  • 第 2 部分:使用 C# 中的代码基于模板生成文档


  • 第 1 部分:创建用于文档生成的模板文档
  • 创建一个示例 XML,您可以根据它创建用于文档生成的 Word 模板。最好从一个不太复杂的版本开始,以掌握它的窍门。

  • 我使用以下 XML 进行测试。为了测试,我没有重复的部分、图片等。


    This is the value in field1 from the XML file
    This is the value in field2 from the XML file
    This is the value in field3 from the XML file


    注释 1 : 这只是一个 示例 XML 创建您的 Word 模板。以后可以在从模板生成 Word 文档时应用具有相同格式的真实数据的 XML 文件。

    注释 2 : xmln属性实际上可以包含您想要的任何内容,并且它不必是以 http 开头的 URL。

    将您的示例 XML 文件保存到任何位置,以便将其导入您将要创建的模板。
  • đảm bảo Nhà phát triển在您的 Word [ Tài liệu 副本上启用了选项卡-> Tùy chọn -> Customize Ribbon -> 下 Customize the Ribbon , 确保 Nhà phát triển被选中 -> ĐƯỢC RỒI ]。详情:How to: Show the Developer Tab on the Ribbon
  • 创建一个新的 Word 文档(或使用现有的 Word 文档),这将是您生成文档的模板。
  • Về Nhà phát triển标签,点击 XML Mapping Pane .这将打开 XML Mapping Pane在文档的右侧。
  • 在 XML 映射 Pane 中,选择 Custom XML Part下拉 -> 选择 (Add new part) .
  • 选择您在步骤 1 中保存的 XML 文件 -> Mở .
  • 在 XML 映射 Pane 中,选择 Custom XML Part下拉 -> 选择带有 xmln 上的文本的项目自定义 XML 文件的属性。如果您使用上面的示例文件,它将是 http://CustomDemoXML.htm .
  • 在 Word 文档中添加一些静态文本并添加 Plain Text Content Control在它旁边(在 Nhà phát triển 选项卡上 -> Controls 部分。对您需要添加的所有字段重复此操作。

  • 对于上面的示例 XML,我有以下 Word 文档:

    sample word document template
  • 点击第一个Plain Text Content Control -> 在 XML 映射 Pane 上,右键单击要映射到该内容控件的字段 -> 单击 Map to Selected Content Control .对要映射的所有字段重复此操作。

  • Ghi chú:或者,而不是添加 Plain Text Content Control步骤 8 中开发人员选项卡中的项目,您可以右键单击要在 XML 映射 Pane 上映射的字段 -> 单击 Insert Content Control -> 点击 Plain Text .

    同样,您还可以添加其他类型的控件,例如复选框、日期选择器甚至重复部分(它也支持嵌套重复部分! - 自 Word 2013 起)并将数据从 XML 映射到仅使用 native Word 功能且无需任何第三方的控件工具!
  • 保存您的模板文档。


  • 第 2 部分:使用 C# 中的代码基于模板生成文档

    这使用了微软推荐的 OpenXML SDK使用包含真实数据的 XML 文件生成文档。
  • 构建您的 XML 文件/打开一个现有的 XML 文件,用它从上面创建的模板生成文档。这需要与用于创建模板的示例 XML 文件的格式相同。
  • 使用 OpenXML SDK 删除任何 CustomXMLPart文档中的元素。这假设文档中没有使用其他自定义 XML 部分,本例就是这种情况。对于复杂的场景,您可以根据需要删除特定的 XML 部分。
  • 使用 OpenXML SDK 添加新的 CustomXMLPart基于上面第 1 步中的 XML 文件。

  • 这是我必须使用包含真实数据的 XML 文件中的数据“刷新”/“重新加载”模板中的示例数据的示例代码(假设用于生成文档的 XML 文件已经创建并保存):
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;

    namespace SampleNamespace
    {
    public static class SampleClass
    {
    public static void GenerateDocument()
    {
    string rootPath = @"C:\Temp";
    string xmlDataFile = rootPath + @"\MyNewData.xml";
    string templateDocument = rootPath + @"\MyTemplate.docx";
    string outputDocument = rootPath + @"\MyGeneratedDocument.docx";

    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(templateDocument, true))
    {
    //get the main part of the document which contains CustomXMLParts
    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

    //delete all CustomXMLParts in the document. If needed only specific CustomXMLParts can be deleted using the CustomXmlParts IEnumerable
    mainPart.DeleteParts(mainPart.CustomXmlParts);

    //add new CustomXMLPart with data from new XML file
    CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
    using (FileStream stream = new FileStream(xmlDataFile, FileMode.Open))
    {
    myXmlPart.FeedData(stream);
    }
    }

    }
    }
    }

    Thế thôi!

    关于c# - 使用 XML 文件中的数据生成 Word 文档 (docx)/基于模板将 XML 转换为 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117531/

    31 4 0
    Chứng chỉ ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
    Xem sitemap của VNExpress