如何将Xml文件导入Word内容控件

时间:2012-09-02 13:31:50

标签: c# xml ms-word

我能够使用映射到Xml架构的内容控件创建一个Word文档,并使用此博客中的代码: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>";

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

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

我打开文档时不再收到错误,但内容控件没有填充?

谢谢。

1 个答案:

答案 0 :(得分:0)

原来我遇到的问题是我的代码和示例实际上没有删除位于&#34; CustomXML&#34;中的以前的CustomXMLParts。夹。我的代码以两种方式工作,第一种是带有一个按钮的Windows窗体应用程序(btnGenerate),它允许您选择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();
        } 
    }
}
}

我希望你们的开发人员和办公室自动化人员能够很好地利用它!