Aspose.Word 操作word表格的行 插入行 添加行
rows.insert或rows.add前row必须有单元格cell
private void button3_Click(object sender, EventArgs e)
{
object savePathWord ="row.docx";
File.Copy("rowtemplate.docx", savePathWord.ToString(),true);
Aspose.Words.Document doc = new Aspose.Words.Document(savePathWord.ToString());
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //拿到所有表格
Aspose.Words.Tables.Table table = allTables[1] as Aspose.Words.Tables.Table; //拿到第二个表格
var row=CreateRow(3,(new string[]{"1","2","3"}),doc); //创建一行
table.Rows.Insert(1,row); //将此行插入第一行的上方
Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc);
Aspose.Words.Tables.Cell c21 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Tables.Cell c22 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Tables.Cell c23 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Paragraph p = new Paragraph(doc);
p.AppendChild(new Run(doc, "测试"));
c21.AppendChild(p);
r2.Cells.Add(c21);
r2.Cells.Add(c22);
r2.Cells.Add(c23);
table.Rows.Add(r2); //添加一行
doc.Save(savePathWord.ToString());
MessageBox.Show("ok");
}
Aspose.Words.Tables.Cell CreateCell(string value,Document doc)
{
Aspose.Words.Tables.Cell c1 = new Aspose.Words.Tables.Cell(doc);
Aspose.Words.Paragraph p = new Paragraph(doc);
p.AppendChild(new Run(doc,value));
c1.AppendChild(p);
return c1;
}
Aspose.Words.Tables.Row CreateRow(int columnCount,string[] columnValues,Document doc)
{
Aspose.Words.Tables.Row r2 = new Aspose.Words.Tables.Row(doc);
for (int i = 0; i < columnCount; i++)
{
if (columnValues.Length >i)
{
var cell = CreateCell(columnValues[i], doc);
r2.Cells.Add(cell);
}
else
{
var cell = CreateCell("", doc);
r2.Cells.Add(cell);
}
}
return r2;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
作者:xuejianxiyang
出处:http://xuejianxiyang.cnblogs.com
关于作者:Heaven helps those who help themselves.
本文版权归原作者和博客园共有,欢迎转载,但未经原作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步