1

Xmlスキーマにマップされたコンテンツコントロールを使用して、次のブログのコードを使用してWordドキュメントを作成することができました:http ://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through- open-xml/ワードドキュメントにデータを挿入できます。

私が持っている質問は、以下のコードを置き換えて、検索結果ごとにこれを記述する代わりに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 documentは入力されず、エラーは発生しませんでした。

-そのコードを次のように置き換えようとしました。//スキーマに一致するXML文字列を作成するカスタムXMLパス文字列newXml=@ "C:\ Users \ Christopher \ Desktop \ BookData \ TestReport.xml"; using(StreamReader sr = new StreamReader(newXml)){newXml = sr.ReadToEnd(); }

ドキュメントを開いたときにエラーが発生しなくなりましたが、コンテンツコントロールにデータが入力されていませんか?

ありがとうございました。

4

1 に答える 1

0

私が遭遇した問題は、私のコードと例が「CustomXML」フォルダーにある以前のCustomXMLPartsを実際に削除していないことでした。コードは2つの方法で機能します。最初の方法は、template.docxファイルとcustomXML.xmlファイルの両方を選択できる1つのボタン(btnGenerate)を備えたWindowsフォームアプリです。2つ目はコンソールプログラムです。乾杯 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 に答える