表格是组织整理数据的一种重要手段,应在生活中的方方面面。在Word文档中将繁杂的文字表述内容表格化,能快速、直接地获取关键内容信息。那么,通过C#,我们也可以在Word文档中添加表格,这里将介绍两种不同的表格添加方法。
使用工具:Spire.Doc for .NET
使用方法:安装后,添加引用dll文件到项目中即可
表格添加方法一:动态地向Word添加表格行和单元格内容,需调用方法section. AddTable()、table. AddRow和row. AddCell()
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
namespace CreateTable_Doc
{
class Program
{
static void Main(string[] args)
{
//创建一个Document类实例,并添加section
Document doc = new Document();
Section section = doc.AddSection();
//添加表格
Table table = section.AddTable(true);
//添加表格第1行
TableRow row1 = table.AddRow();
//添加第1个单元格到第1行
TableCell cell1 = row1.AddCell();
cell1.AddParagraph().AppendText("序列号");
//添加第2个单元格到第1行
TableCell cell2 = row1.AddCell();
cell2.AddParagraph().AppendText("设备名称");
//添加第3个单元格到第1行
TableCell cell3 = row1.AddCell();
cell3.AddParagraph().AppendText("设备型号");
//添加第4个单元格到第1行
TableCell cell4 = row1.AddCell();
cell4.AddParagraph().AppendText("设备数量");
//添加第5个单元格到第1行
TableCell cell5 = row1.AddCell();
cell5.AddParagraph().AppendText("设备价格");
//添加表格第2行
TableRow row2 = table.AddRow(true, false);
//添加第6个单元格到第2行
TableCell cell6 = row2.AddCell();
cell6.AddParagraph().AppendText("1");
//添加第7个单元格到第2行
TableCell cell7 = row2.AddCell();
cell7.AddParagraph().AppendText("机床");
//添加第8个单元格到第2行
TableCell cell8 = row2.AddCell();
cell8.AddParagraph().AppendText("M170010");
//添加第9个单元格到第2行
TableCell cell9 = row2.AddCell();
cell9.AddParagraph().AppendText("12");
//添加第10个单元格到第2行
TableCell cell10 = row2.AddCell();
cell10.AddParagraph().AppendText("8W");
table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
//保存文档
doc.SaveToFile("Table.docx");
}
}
}
表格添加方法二:预定义表格行和列
using System;
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;
namespace CreateTable2_Word
{
class Program
{
static void Main(string[] args)
{
//创建一个Document类实例,并添加section
Document document = new Document();
Section section = document.AddSection();
//添加表格指定表格的行数和列数(2行,5列)
Table table = section.AddTable(true);
table.ResetCells(2, 5);
//获取单元格(第1行第1个单元格)并添加文本内容,设置字体字号颜色等(单元格中内容及个性化设置可以根据需要来进行调整)
TextRange range = table[0, 0].AddParagraph().AppendText("序列号");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Brown;
range.CharacterFormat.Bold = true;
//获取单元格(第1行第2个单元格)并添加文本
range = table[0, 1].AddParagraph().AppendText("设备名称");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Brown;
range.CharacterFormat.Bold = true;
//获取单元格(第1行第3个单元格)并添加文本
range = table[0, 2].AddParagraph().AppendText("设备型号");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Brown;
range.CharacterFormat.Bold = true;
//获取单元格(第1行第4个单元格)并添加文本
range = table[0, 3].AddParagraph().AppendText("设备数量");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Brown;
range.CharacterFormat.Bold = true;
//获取单元格(第1行第5个单元格)并添加文本
range = table[0, 4].AddParagraph().AppendText("设备价格");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
range.CharacterFormat.TextColor = Color.Brown;
range.CharacterFormat.Bold = true;
//获取单元格(第2行第1个单元格)并添加文本
range = table[1, 0].AddParagraph().AppendText("1");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
//获取单元格(第2行第2个单元格)并添加文本
range = table[1, 1].AddParagraph().AppendText("机床");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
//获取单元格(第2行第3个单元格)并添加文本
range = table[1, 2].AddParagraph().AppendText("M170010");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
//获取单元格(第2行第4个单元格)并添加文本
range = table[1, 3].AddParagraph().AppendText("12");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
//获取单元格(第2行第5个单元格)并添加文本
range = table[1, 4].AddParagraph().AppendText("8W");
range.CharacterFormat.FontName = "Arial";
range.CharacterFormat.FontSize = 12;
//保存文档
document.SaveToFile("Table2.docx");
}
}
}
以上介绍的两种方法中,你可以根据自己的需要添加内容或者设置内容格式等。如果觉得对你有用的话,欢迎转载!感谢阅读。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。