简体   繁体   English

使用Microsoft Work Automation C#添加动态表行

[英]Add dynamic table row using microsoft work automation c#

I have a word document template which contains several form fields that need to be filled using c# code. 我有一个Word文档模板,其中包含几个需要使用C#代码填写的表单字段。

below is the document image 下面是文档图像 在此处输入图片说明 The code below is used to reach and fill the document form fields, But when i reach the table sections sometimes the rows need to be filled are more than what is pre defined inside the template. 下面的代码用于到达和填充文档表单字段,但是当我到达表格部分时,有时需要填充的行比模板中预先定义的要多。

red marked area is the table which i want to fill it with data and create as many rows as needed. 红色标记的区域是我要用数据填充并创建所需行数的表。

the code i use for filling the data is 我用于填充数据的代码是

        string fileName = Path.GetTempFileName();
        File.WriteAllBytes(fileName, Properties.Resources.DocumentTemplate);
        Word.Application word = new Word.Application();
        Word.Document doc = new Word.Document();
        doc = word.Documents.Add(fileName);
        doc.Activate();


        doc.FormFields["file_num"].Range.Text = "some text";
        doc.FormFields["fam_size"].Range.Text = "another text";
        doc.FormFields["nationality"].Range.Text = "another text";
        for(int i =0; i< rowsInDatabaseCount; i++)
        {
           //i don't know how to add row and reach each form field inside
        }

I hope someone can help me on how to achieve what i want. 我希望有人能帮助我实现我想要的目标。

Thank you in advance... 先感谢您...

There are multiple ways to handle that. 有多种处理方法。 1) Since the data is coming from a database, one way is to use InsertDatabase method. 1)由于数据来自数据库,因此一种方法是使用InsertDatabase方法。

2) You could insert the block as tab or comma separated text then convert to a table using ConvertToTable() method. 2)您可以将块插入为制表符或逗号分隔的文本,然后使用ConvertToTable()方法转换为表。

3) You might use Rows and Cols collections (of Table) and .Add method to add new rows. 3)您可能使用Rows和Cols集合(表的)和.Add方法来添加新行。

4) You might instead create your template as an XSL and transform data (XML) using that XSL to generate final HTM. 4)您可以改为将模板创建为XSL,并使用该XSL转换数据(XML)以生成最终的HTM。 Word is able to open an HTM file. Word能够打开HTM文件。

(All these options worked for me in "resume", "test results" ... creations which have similar layouts to the ones you gave. I found final option 4 to be the most dynamic one, where end users could define their own templates using simple HTML editors - worked better than word templates for me). (所有这些选项在“恢复”,“测试结果” ...中的工作方式对我都有效。...创建的布局与您提供的布局相似。我发现最终的选项4是最动态的,最终用户可以在其中定义自己的模板使用简单的HTML编辑器-对我来说,效果比单词模板更好)。

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

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