【问题标题】:How do import Xml file into Word Content Controls如何将 Xml 文件导入 Word 内容控件
【发布时间】:2012-09-02 13:31:49
【问题描述】:

我能够创建一个 Word 文档,其中包含映射到 Xml 架构的内容控件,并使用此博客中的代码:http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/ 我能够将数据插入到 Word 文档中。

我的问题是,是否可以替换下面的代码,这样我就可以使用 Xml 文件,而不必为每个发现都写这个:

//create XML string matching schema custom XML path
            string newXml = "<root>" +
                "<FINDING>Adobe Flash Player contains multiple...</FINDING>" +
                "<STATUS>Open</STATUS>" +
                "<THREATLEVEL>High</THREATLEVEL>" +
                "<RECOMMENDATION>Update Flash Player to version...</RECOMMENDATION>" +
                "<DEVICEAFFECTED>UserPC</DEVICEAFFECTED>" +
                "<SCANNER>XXXXXX</SCANNER>" +
                "</root>";

我尝试将其替换为: 字符串 newXml = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml"; 并使用 StreamReader 和现有的 StreamWriter 创建了一个嵌套的 using 语句,但不会填充 word 文档,也不会出现任何错误。

--我只是尝试用以下代码替换该代码: //创建XML字符串匹配模式自定义XML路径 字符串 newXml = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml"; 使用 (StreamReader sr = new StreamReader (newXml)) { newXml = sr.ReadToEnd(); }

当我打开文档时不再收到错误消息,但内容控件未填充?

谢谢。

【问题讨论】:

    标签: c# xml ms-word


    【解决方案1】:

    原来我遇到的问题是我的代码和示例实际上并没有删除位于“CustomXML”文件夹中的以前的 CustomXMLParts。我的代码以两种方式工作,第一种是带有一个按钮 (btnGenerate) 的 Windows 窗体应用程序,它允许您选择 template.docx 和 customXML.xml 文件。第二个是控制台程序。 干杯 Windows 窗体应用程序:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Threading;
    
    
    namespace TestReportCreator_Beta
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        [STAThread]
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string outFile = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
    
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Multiselect = false;
            OFD.Title = "Open Word Document";
            OFD.Filter = "Word Document|*.docx;*.domx";
            OFD.ShowDialog();
            string docPath = OFD.FileName;
            OFD.Title = "Opne Xml Document";
            OFD.Filter = "Xml Document|*.xml";
            OFD.ShowDialog();
            string xmlPath = OFD.FileName;
    
            // convert template to document
            File.Copy(docPath, outFile);
    
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream fs = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            } 
        }
    }
    }
    

    这是控制台程序版本

    using System; using System.Collections.Generic;
    using System.Linq; using System.Text;
    using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing; using System.IO;
    
    namespace BookData
    {
    class Program
    {
    
        static void Main(string[] args)
        {
            string template = @"C:\Users\Christopher\Desktop\BookData\TestReportBeta.docx";
            string outFile = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
            string xmlPath = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml";
    
            // convert template to document
            File.Copy(template, outFile);
    
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts<CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream fs = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            } 
        }
    }
    }
    

    我希望你们那里的开发人员和办公自动化人员能够充分利用它!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 2014-07-26
      • 2020-06-30
      相关资源
      最近更新 更多