简体   繁体   English

使用OpenXml SDK 2.0在Word文档中插入表格

[英]Using the OpenXml SDK 2.0 to insert tables in a word document

I am just starting out with the OpenXML SDK 2.0 in Visual Studio 2010 (C#). 我只是从Visual Studio 2010(C#)中的OpenXML SDK 2.0开始。 I have automated office programs before using COM automation, which was painful. 在使用COM自动化之前,我已经有了自动化的办公程序,这很痛苦。

I have a template made by one of our graphic designers, which will provide the foundation for my reports. 我有一个由我们的图形设计师制作的模板,该模板将为我的报告奠定基础。 In order to automate the simple things (plaintext items) I have added content controls to the template and bound a custom XML part to the doc. 为了使简单的事情(纯文本项)自动化,我向模板添加了内容控件,并将自定义XML部件绑定到文档。 The content controls are as follows: 内容控件如下:

  • DayCount DayCount
  • AlternateJobTitle 替代工作标题
  • Date 日期
  • SignatureName 签名名

After making a copy of the template, I then edit the content controls and save the file with the following code: 复制完模板后,我然后编辑内容控件并使用以下代码保存文件:

//stand up object that reads the Word doc package
using (WordprocessingDocument doc = WordprocessingDocument.Open(docOutputPath, true))
  {
    //create XML string matching custom XML part
    string newXml = "<root>" +
                    "<DayCount>42</DayCount>" +
                    "<AlternateJobTitle>Supervisor</AlternateJobTitle>" +
                    "<Date>9/24/2012</Date>" +
                    "<SignatureName>John Doe</SignatureName>" +
                    "</root>";

    MainDocumentPart main = doc.MainDocumentPart;
    main.DeleteParts<CustomXmlPart>(main.CustomXmlParts);

    //add and write new XML part
    CustomXmlPart customXml = main.AddCustomXmlPart(CustomXmlPartType.CustomXml);
    using (StreamWriter ts = new StreamWriter(customXml.GetStream()))
    {

       ts.Write(newXml);
    }
}

This all works well. 这一切都很好。 However, my document is not made up solely of standard text and plaintext updates. 但是,我的文档不是仅由标准文本和纯文本更新组成。 The real meat of the report is in a number of tables that need to be added to each report as well. 报告的真实内容在许多表中,这些表也需要添加到每个报告中。 I have been searching like crazy for a good description on how this is done, but have really not found anything. 我一直在疯狂地寻找如何完成此操作的良好描述,但实际上什么也没找到。 Is there some way to delineate where to place a table using the same content control logic used for plaintext controls? 有什么方法可以使用与纯文本控件相同的内容控件逻辑来描述将表放置在何处? Any code samples I have found of creating a table using OpenXML have just assumed that you want to append it to the end of the main document part. 我发现使用OpenXML创建表的任何代码示例都只是假定您要将其附加到主文档部分的末尾。 I would like to specify where the tables need to go in the template, generate the tables and place them in the specified regions of the template. 我想指定表格在模板中的位置,生成表格并将它们放置在模板的指定区域中。 Is this possible? 这可能吗?

Any help is greatly appreciated. 任何帮助是极大的赞赏。

There are a lot of OpenXml creation questions. 有很多OpenXml创建问题。 But if you decide to take this path - answer is general - examine OpenXml Productivity Tool . 但是,如果您决定走这条路,答案是一般的,请检查OpenXml生产力工具 At my PC it could be found at "C:\\Program Files (x86)\\Open XML SDK\\V2.0\\tool\\OpenXmlSdkTool.exe". 在我的PC上,可以找到“ C:\\ Program Files(x86)\\ Open XML SDK \\ V2.0 \\ tool \\ OpenXmlSdkTool.exe”。 Just create in MsWord document which you want to create using OpenXml and reflect document's code using this tool. 只需在要使用OpenXml创建的MsWord文档中创建并使用此工具反映文档的代码即可。 Good luck! 祝好运!

到目前为止,如果您需要显示表格数据,我发现最好的东西是位于http://worddocgenerator.codeplex.com/的 Word Document Generator。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM