轉(zhuǎn)帖|使用教程|編輯:李顯亮|2019-08-16 14:29:55.543|閱讀 1907 次
概述:本系列教程將為大家?guī)?來Spire.Doc for .NET在使用過程中的各類實(shí)際操作,本篇文章介紹了如何設(shè)置 Word 表格的格式。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一個(gè)專業(yè)的Word .NET庫,設(shè)計(jì)用于幫助開發(fā)人員高效地開發(fā)創(chuàng)建、閱讀、編寫、轉(zhuǎn)換和打印任何來自.NET( C#, VB.NET, ASP.NET)平臺(tái)的Word文檔文件的功能。
本系列教程將為大家?guī)?strong>Spire.Doc for .NET在使用過程中的各類實(shí)際操作,本篇文章介紹了如何設(shè)置 Word 表格的格式。>>下載Spire.Doc最新試用版體驗(yàn)
在Word文檔中,表格可以幫助我們清晰、直觀的分析和整理數(shù)據(jù)。一個(gè)表格通常至少包含一行,每行至少包含一個(gè)單元格,一個(gè)單元格可以包含多種元素如文本和表格(即嵌套表格)等。
添加和插入行
*添加和插入行
//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];
//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;
//添加一行到表格的最后
table.AddRow(true, 4);
//插入一行到表格的第三行
table.Rows.Insert(2, table.AddRow());
//保存文檔
document.SaveToFile("AddRow.docx", FileFormat.Docx2013);
*添加列
//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)節(jié)
Section section = document.Sections[0];
//獲取第一個(gè)表格
Table table = section.Tables[0] as Table;
//添加一列到表格,設(shè)置單元格的寬度和寬度類型
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);
設(shè)置行高和列寬
//載入文檔
Document document = new Document("Table.docx");
//獲取第一個(gè)表格
Table table = document.Sections[0].Tables[0] as Table;
//設(shè)置第一行的行高
table.Rows[0].Height = 40;
for (int i = 0; i < table.Rows.Count; i++)
{
    //設(shè)置第二列的列寬
    table.Rows[i].Cells[1].Width = 40;
}
//保存文檔
document.SaveToFile("SetRowHeightAndColumnWidth.docx", FileFormat.Docx2013);
刪除行和列
//載入文檔
Document doc = new Document("Table.docx");
//獲取第一個(gè)表格
Table table = doc.Sections[0].Tables[0] as Table;
//刪除第二行
table.Rows.RemoveAt(1);
//刪除第二列
for (int i = 0; i < table.Rows.Count; i++)
{
    table.Rows[i].Cells.RemoveAt(1);
}
//保存文檔
doc.SaveToFile("RemoveRowAndColumn.docx", FileFormat.Docx2013);
*購買Spire.Doc正版授權(quán)的朋友可以點(diǎn)擊哦~~
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn