Aspose.Words - 表格行复制、插入及编辑

Aspose.Words的Table, Row, Cell三级对象,依次实现对表格、表格行、行内列单元的三级封装处理。

Aspose.Words - 表格行复制、插入及编辑

1 Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Aspose.Words;
using Aspose.Words.Tables;
using System;
using System.Text.RegularExpressions;

namespace TableTest
{
class Program
{
static string MyDir = "C:\\Dev\\Test\\";
static void Main(string[] args)
{
Document doc = new Document(MyDir + "in.docx");

// Retrieve the second([0,1,...]) table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 1, true);

int rowCount = 0;
foreach (Row row in table.Rows)
{
// 第0行作为标题行,第1行作为内容模板行
if(rowCount == 1)
{
Row rowClone = (Row)row.Clone(true);

row.Range.Replace(new Regex("__newName__"), "Name1");
row.Range.Replace(new Regex("__newPrice__"), "111");

row.ParentNode.InsertAfter(rowClone, row); // insert the template row after current edited row
}

rowCount++;
}
doc.Save(MyDir + "out.docx");
}
}
}