Nic*_*ole 14 c# ms-word openxml
我需要使用现有表(例如,3列)打开现有Word文档(.docx)并向该表添加新行.有没有办法做到这一点?我正在使用Open XML
我正在创建这样的表(第一次):
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;
Run Code Online (Sandbox Code Playgroud)
jn1*_*1kk 17
干得好,
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"))))));
}
Run Code Online (Sandbox Code Playgroud)
使用LINQ获取正确的表.
编辑:
假设您想要获得包含4列的表.
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"))))));
}
Run Code Online (Sandbox Code Playgroud)
假设您要获取包含单词"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"))))));
}
Run Code Online (Sandbox Code Playgroud)
下面是一个更详细的示例,您可以在现有表中添加5行.
这假设该表是文档中的第一个表.如果没有,你必须找到你的桌子.
代码获取表的最后一行并复制它.之后,您只需要在单元格中填写数据.
Table myTable = doc.Body.Descendants<Table>().First();
TableRow theRow = myTable.Elements<TableRow>().Last();
for (int i = 0; i < 5; i++)
{
TableRow rowCopy = (TableRow)theRow.CloneNode(true);
var runProperties = GetRunPropertyFromTableCell(rowCopy, 0);
var run = new Run(new Text(i.ToString() + " 1"));
run.PrependChild<RunProperties>(runProperties);
rowCopy.Descendants<TableCell>().ElementAt(0).RemoveAllChildren<Paragraph>();//removes that text of the copied cell
rowCopy.Descendants<TableCell>().ElementAt(0).Append(new Paragraph(run));
//I only get the the run properties from the first cell in this example, the rest of the cells get the document default style.
rowCopy.Descendants<TableCell>().ElementAt(1).RemoveAllChildren<Paragraph>();
rowCopy.Descendants<TableCell>().ElementAt(1).Append(new Paragraph(new Run(new Text(i.ToString() + " 2"))));
rowCopy.Descendants<TableCell>().ElementAt(2).RemoveAllChildren<Paragraph>();
rowCopy.Descendants<TableCell>().ElementAt(2).Append(new Paragraph(new Run(new Text(i.ToString() + " 3"))));
myTable.AppendChild(rowCopy);
}
myTable.RemoveChild(theRow); //you may want to remove this line. I have it because in my code i always have a empty row last in the table that i copy.
Run Code Online (Sandbox Code Playgroud)
GetRunPropertiesFromTableCell是我尝试使用与现有行相同的文本格式的快速黑客尝试.
private static RunProperties GetRunPropertyFromTableCell(TableRow rowCopy, int cellIndex)
{
var runProperties = new RunProperties();
var fontname = "Calibri";
var fontSize = "18";
try
{
fontname =
rowCopy.Descendants<TableCell>()
.ElementAt(cellIndex)
.GetFirstChild<Paragraph>()
.GetFirstChild<ParagraphProperties>()
.GetFirstChild<ParagraphMarkRunProperties>()
.GetFirstChild<RunFonts>()
.Ascii;
}
catch
{
//swallow
}
try
{
fontSize =
rowCopy.Descendants<TableCell>()
.ElementAt(cellIndex)
.GetFirstChild<Paragraph>()
.GetFirstChild<ParagraphProperties>()
.GetFirstChild<ParagraphMarkRunProperties>()
.GetFirstChild<FontSize>()
.Val;
}
catch
{
//swallow
}
runProperties.AppendChild(new RunFonts() { Ascii = fontname });
runProperties.AppendChild(new FontSize() { Val = fontSize });
return runProperties;
}
Run Code Online (Sandbox Code Playgroud)