添加一行到现有表在Word文档中(开放式XML) [英] Add a row to an existing table in a Word Document (open XML)

查看:351
本文介绍了添加一行到现有表在Word文档中(开放式XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要与现有的表打开现有的Word文档(.docx)(有,例如,3列),并添加一个新行到该表。是否有这样做的方法吗?我使用Open XML



我创建这样的表(第一时间):

 表TBL =新表(); 

//设置表的样式和宽度。
TableProperties tableProp =新TableProperties();
TABLESTYLE TABLESTYLE =新TABLESTYLE(){瓦尔=TableGrid};

//使工作台宽度页面宽度的100%。
TableWidth tableWidth =新TableWidth(){WIDTH =5000,类型= TableWidthUnitValues.Pct};

//应用
tableProp.Append(TABLESTYLE,tableWidth);
tbl.AppendChild(tableProp);

//添加3列到表中。
TableGrid TG =新TableGrid(新的GridColumn(),新的GridColumn(),新的GridColumn());
tbl.AppendChild(TG);

//创建1行的表。
的TableRow TR1 =新的TableRow();

//小区添加到该行中的每个列。
的TableCell TC1 =新的TableCell(新段(新润(新文本(1))));
TableCell的TC2 =新的TableCell(新段(新润(新文本(2))));
TableCell的TC3 =新的TableCell(新段(新润(新文本(3))));
tr1.Append(TC1,TC2,TC3);

//添加一行到表中。
tbl.AppendChild(TR1);
返回TBL;


解决方案

在这里,你走了,

 体BOD = doc.MainDocumentPart.Document.Body; 
的foreach(在bod.Descendants<表t;表>())
{
t.Append(新的TableRow(新的TableCell(新段(新润(新文本(测试) )))));
}

使用LINQ得到适当的表。



编辑:



假设你想要得到的有4个列的表

 体BOD = doc.MainDocumentPart.Document.Body; 
的foreach(在bod.Descendants<表t;表>()式(TBL =方式> tbl.GetFirstChild<的TableRow>()后裔< TableCell的方式>()计数()== 4))
{
t.Append(新的TableRow(新的TableCell(新段(新润(新文本(测试))))));
}



假设你想要得到包含单词MYTABLE表。

 体BOD = doc.MainDocumentPart.Document.Body; 
的foreach(在bod.Descendants<表t;表>()式(TBL => tbl.InnerText.Contains(myTable的)))
{
t.Append(新的TableRow(新的TableCell(新段(新润(新文本(测试))))));
}


I need to open an existing Word document (.docx) with an existing table (with, for example, 3 columns) and add a new row to that table. Is there any way of doing this? I am using Open XML

I am creating the table like this (for the first time):

Table tbl = new Table();

// Set the style and width for the table.
TableProperties tableProp = new TableProperties();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

// Make the table width 100% of the page width.
TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };

// Apply
tableProp.Append(tableStyle, tableWidth);
tbl.AppendChild(tableProp);

// Add 3 columns to the table.
TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
tbl.AppendChild(tg);

// Create 1 row to the table.
TableRow tr1 = new TableRow();

// Add a cell to each column in the row.
TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1"))));
TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2"))));
TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3"))));
tr1.Append(tc1, tc2, tc3);

// Add row to the table.
tbl.AppendChild(tr1);
return tbl;

解决方案

Here you go,

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>())
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}

Use LINQ to get the proper table.

EDIT:

Say you want to get the table that has 4 columns.

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.GetFirstChild<TableRow>().Descendants<TableCell>().Count() == 4))
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}

Say you want to get the table that contains the word "mytable".

Body bod = doc.MainDocumentPart.Document.Body;
foreach (Table t in bod.Descendants<Table>().Where(tbl => tbl.InnerText.Contains("myTable")))
{
    t.Append(new TableRow(new TableCell(new Paragraph(new Run(new Text("test"))))));
}

这篇关于添加一行到现有表在Word文档中(开放式XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆