c# aspose.words word表格都是添加行,怎么添加列?

c# aspose.words word表格都是添加行,怎么添加列?

下面的代码是java的,稍微修改即可适合C#
添加列需要遍历每一行,然后在最后一个单元格后面再插入一个单元格

//Open document

Document doc = new Document("C:\\Temp\\in.doc");


//Get table from the document

Table tab = doc.getFirstSection().getBody().getTables().get(0);


//Add few more columns

for(int i=0; i<3; i++)

{

for(Row row : tab.getRows())

{

//clone las cell of the row and append it to the row

Node cellClone = row.getLastCell().deepClone(true);

row.appendChild(cellClone);

}

}


//Save output document

doc.save("C:\\Temp\\out.doc");

可以参考spire.doc for .net 的表格列添加方法:

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section = document.Sections[0];

//获取第一个表格
Table table = section.Tables[0] as Table;

//添加一列到表格,设置单元格的宽度和宽度类型
for (int i = 0; i < table.Rows.Count; i++)
{
    TableCell cell = table.Rows[i].AddCell(true);
    cell.Width = table[0, 0].Width;
    cell.CellWidthType = table[0, 0].CellWidthType;
}

//保存文档
document.SaveToFile("AddColumn.docx", FileFormat.Docx2013);

原文:https://www.e-iceblue.cn/spiredoc/working-with-rows-and-columns-of-word-table.html