1

我能够创建一个 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>";

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

--我只是尝试用以下代码替换该代码: //create XML string matching schema custom XML path string newXml = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml"; 使用 (StreamReader sr = new StreamReader (newXml)) { newXml = sr.ReadToEnd(); }

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

谢谢你。

4

1 回答 1

0

原来我遇到的问题是我的代码和示例实际上并没有删除位于“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();
        } 
    }
}
}

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

于 2012-09-17T21:55:07.467 回答