表格是组织整理数据的一种重要手段,应在生活中的方方面面。在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");
}
}
}
以上介绍的两种方法中,你可以根据自己的需要添加内容或者设置内容格式等。如果觉得对你有用的话,欢迎转载!感谢阅读。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。