C# 操作现有Word表格详细教程

C# 操作现有Word表格详细教程

在Word中,我们可以给一个表格设置格式(如设置样式,设置边框,设置单元格背景色),添加或删除行、列,设置行高和列宽,合并和拆分单元格,插入图片等,也可以删除该表格。在这篇文章中我将介绍如何使用C#和Spire.Doc for .NET控件来实现这些操作。


Spire.Doc组件概述

Spire.Doc是一款专门对 Word 文档进行操作的 .NET类库,用于在.NET应用程序中创建、编辑、打印和转换Word文档,它支持 Word97-2003,Word2007,Word2010 以及 Word2013,并且使用时无需安装Microsoft Word。支持几乎所有的 Word 文档元素,主要包括页面、节、页眉、页脚、脚注、尾注、段落、项目符号和编号、表格、文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、文档设置和文档保护。同时,也支持形状、文本框、图片、OLE 对象和内容控件。

下载安装Spire.Doc组件后,需添加引用Spire.Doc.dll到Visual Studio,并在程序中添加相应命名空间。


操作Word表格

Spire.Doc组件提供了一个类叫做Table,一个Table对象代表了word文档中的一个表格,通过Table对象可以调用一些方法和属性设置来对Word中的表格进行相应的操作。下面我将展示如何使用Spire.Doc来操作Word文档中的现有表格。

1、表格样式设置

内置样式设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//给表格应用内置样式
table.ApplyStyle(DefaultTableStyle.LightGridAccent3);
 
//保存文档
document.SaveToFile("BuiltinStyle.docx", FileFormat.Docx2013);

自定义段落样式设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//设置自定义样式
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "TableStyle";           
style.CharacterFormat.FontSize= 14;
style.CharacterFormat.TextColor= Color.SeaGreen;
style.CharacterFormat.HighlightColor= Color.Yellow;
//将自定义样式添加到文档
document.Styles.Add(style);
 
//给表格第一行第一个单元格的第一个段落应用自定义样式
table[0,0].Paragraphs[0].ApplyStyle(style.Name);
 
//保存文档
document.SaveToFile("CustomStyle.docx", FileFormat.Docx2013);

自适应方式设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//自适应内容
table.AutoFit(AutoFitBehaviorType.AutoFitToContents);
//自适应窗口
//table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
//固定列宽
//table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);
 
//保存文档
document.SaveToFile("AutofitMode.docx", FileFormat.Docx2013);


2、表格对齐方式设置

//载入文档
Document document = new Document("Tables.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一、二、三个表格
Table table1 =section.Tables[0] as Table;
Table table2 =section.Tables[1] as Table;
Table table3 =section.Tables[2] as Table;
 
//设置第一个表格居左
table1.TableFormat.HorizontalAlignment= RowAlignment.Left;
table1.TableFormat.LeftIndent= 34;
 
//设置第二个表格居中
table2.TableFormat.HorizontalAlignment= RowAlignment.Center;
table2.TableFormat.LeftIndent= 34;
 
//设置第三个表格居右
table3.TableFormat.HorizontalAlignment= RowAlignment.Right;
table3.TableFormat.LeftIndent= 34;
 
//保存文档
document.SaveToFile("TableAlignment.docx", FileFormat.Docx2013);


3、边框设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//设置表格的上边框
table.TableFormat.Borders.Top.BorderType= BorderStyle.Double;
table.TableFormat.Borders.Top.LineWidth= 1.0F;
table.TableFormat.Borders.Top.Color= Color.YellowGreen;
 
//设置表格的左边框
table.TableFormat.Borders.Left.BorderType= BorderStyle.Double;
table.TableFormat.Borders.Left.LineWidth= 1.0F;
table.TableFormat.Borders.Left.Color= Color.YellowGreen;
 
//设置表格的右边框
table.TableFormat.Borders.Right.BorderType= BorderStyle.Double;
table.TableFormat.Borders.Right.LineWidth= 1.0F;
table.TableFormat.Borders.Right.Color= Color.YellowGreen;
 
//设置表格的下边框
table.TableFormat.Borders.Bottom.BorderType= BorderStyle.Double;
table.TableFormat.Borders.Bottom.LineWidth= 1.0F;
table.TableFormat.Borders.Bottom.Color= Color.YellowGreen;
 
//设置表格的水平和垂直边框
table.TableFormat.Borders.Horizontal.BorderType= BorderStyle.Hairline;
table.TableFormat.Borders.Horizontal.Color= Color.Orange;
table.TableFormat.Borders.Vertical.BorderType= BorderStyle.Hairline;           
table.TableFormat.Borders.Vertical.Color= Color.Orange;
 
//保存文档
document.SaveToFile("TableBorder.docx", FileFormat.Docx2013);


4、背景颜色设置

行背景颜色设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
//设置第一行的背景颜色
table.Rows[0].RowFormat.BackColor= Color.SeaGreen;
 
//保存文档
document.SaveToFile("RowBackColor.docx", FileFormat.Docx2013);

单元格背景颜色设置

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
//设置第一行第一个单元格的背景颜色
table[0,0].CellFormat.BackColor= Color.SeaGreen;
 
//保存文档
document.SaveToFile("CellBackColor.docx", FileFormat.Docx2013);


5、单元格段落对齐方式设置

水平对齐方式设置

//载入文档
Document document = new Document("Table1.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//设置表格的第二行水平居左                       
table[1,0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;
table[1,1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;
table[1,2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Left;
 
//设置表格的第三行水平居中
table[2,0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
table[2,1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
table[2,2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
 
//设置表格的第四行水平居右
table[3,0].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;
table[3,1].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;
table[3,2].Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Right;
 
//保存文档
document.SaveToFile("HorizontalAlignment.docx", FileFormat.Docx2013);

垂直对齐方式设置

//载入文档
Document document = new Document("Table1.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
Table table =section.Tables[0] as Table;
 
//设置表格第二行垂直居上
table[1,0].CellFormat.VerticalAlignment= VerticalAlignment.Top;
table[1,1].CellFormat.VerticalAlignment= VerticalAlignment.Top;
table[1,2].CellFormat.VerticalAlignment= VerticalAlignment.Top;
 
//设置表格第三行垂直居中
table[2,0].CellFormat.VerticalAlignment= VerticalAlignment.Middle;
table[2,1].CellFormat.VerticalAlignment= VerticalAlignment.Middle;
table[2,2].CellFormat.VerticalAlignment= VerticalAlignment.Middle;
 
//设置表格第四行垂直居下
table[3,0].CellFormat.VerticalAlignment= VerticalAlignment.Bottom;
table[3,1].CellFormat.VerticalAlignment= VerticalAlignment.Bottom;
table[3,2].CellFormat.VerticalAlignment= VerticalAlignment.Bottom;
 
//保存文档
document.SaveToFile("VerticalAlignment.docx", FileFormat.Docx2013);


6、添加行和列

添加/插入行

//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section =document.Sections[0];
 
//获取第一个表格
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");
//获取第一个节
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);


7、删除行和列

//载入文档
Document doc = new Document("Table.docx");
 
//获取第一个表格
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);


8、设置行高和列宽

//载入文档
Document document = new Document("Table.docx");
 
//获取第一个表格
Table table =document.Sections[0].Tables[0] as Table;
 
//设置第一行的行高
table.Rows[0].Height= 20;
 
for (int i = 0; i < table.Rows.Count; i++)
{
    //设置第二列的列宽
    table.Rows[i].Cells[1].Width = 20;
}
 
//保存文档
document.SaveToFile("SetRowHeightAndColumnWidth.docx", FileFormat.Docx2013);


9、插入图片到单元格

//载入文档
Document document = new Document("Product.docx");
 
//获取第一个表格
Table table =document.Sections[0].Tables[0] as Table;
 
//插入图片到第二行第二个单元格
DocPicture picture =table.Rows[1].Cells[1].Paragraphs[0].AppendPicture(Image.FromFile("1.jpg"));
//设置图片的宽度和高度
picture.Width = 100;
picture.Height =100;
 
//保存文档
document.SaveToFile("InsertPicture.docx", FileFormat.Docx2013);


10、合并和拆分单元格

合并单元格

//载入文档
Document document = new Document("Table.docx");
 
//获取第一个表格
Table table =document.Sections[0].Tables[0] as Table;
 
//水平合并(合并第1行的第1、2个单元格)
table.ApplyHorizontalMerge(0,0, 1);           
table.Rows[0].Cells[0].Paragraphs[0].ChildObjects.Add(table.Rows[0].Cells[1].Paragraphs[0].ChildObjects[0]);
 
//垂直合并(合并第1列的第3、4个单元格)
table.ApplyVerticalMerge(0,2, 3);            
table.Rows[2].Cells[0].Paragraphs[0].ChildObjects.Add(table.Rows[3].Cells[0].Paragraphs[0].ChildObjects[0]);
 
//保存文档
document.SaveToFile("MergeCells.docx", FileFormat.Docx);


拆分单元格

Document document = new Document("Table.docx");
 
Table table =document.Sections[0].Tables[0] as Table;
 
//将第4行的第4个单元格拆分为3列2行           
table.Rows[3].Cells[3].SplitCell(3,2);
 
//保存文档
document.SaveToFile("SplitCells.docx", FileFormat.Docx);


11、插入嵌套表格

//载入文档
Document document = new Document("Table.docx");
//获取第一个表格
Table table =document.Sections[0].Tables[0] as Table;
 
//添加嵌套表格到该表格的第4行第4列的单元格 
Table nestedTable =table[3, 3].AddTable(true);
 
//指定嵌套表格的行数和列数 
nestedTable.ResetCells(2,2);
 
//给嵌套表格应用格式 
nestedTable.ApplyStyle(DefaultTableStyle.LightListAccent4);
nestedTable.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
 
//保存文档
document.SaveToFile("NestedTable.docx", FileFormat.Docx2013);


12、删除表格

//载入文档
Document document = new Document(@"Table.docx");
 
//删除第一个表格           
document.Sections[0].Tables.RemoveAt(0);
 
//保存文档
document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);


 

  • 11
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值