-
C# 在Word中添加表格的方法
表格是组织整理数据的一种重要手段,应在生活中的方方面面。在Word文档中将繁杂的文字表述内容表格化,能快速、直接地获取关键内容信息。那么,通过C#,我们也可以在Word文档中添加表格,这里将介绍两种不同的表格添加方法。
使用工具:Spire.Doc for .NET https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html
使用方法:安装后,添加引用dll文件到项目中即可
表格添加方法一:动态地向Word添加表格行和单元格内容,需调用方法section. AddTable()、table. AddRow和row. AddCell()

1 using System; 2 using Spire.Doc; 3 using Spire.Doc.Documents; 4 using Spire.Doc.Fields; 5 using System.Drawing; 6 7 8 namespace CreateTable_Doc 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //创建一个Document类实例,并添加section 15 Document doc = new Document(); 16 Section section = doc.AddSection(); 17 18 //添加表格 19 Table table = section.AddTable(true); 20 21 //添加表格第1行 22 TableRow row1 = table.AddRow(); 23 24 //添加第1个单元格到第1行 25 TableCell cell1 = row1.AddCell(); 26 cell1.AddParagraph().AppendText("序列号"); 27 28 //添加第2个单元格到第1行 29 TableCell cell2 = row1.AddCell(); 30 cell2.AddParagraph().AppendText("设备名称"); 31 32 //添加第3个单元格到第1行 33 TableCell cell3 = row1.AddCell(); 34 cell3.AddParagraph().AppendText("设备型号"); 35 36 //添加第4个单元格到第1行 37 TableCell cell4 = row1.AddCell(); 38 cell4.AddParagraph().AppendText("设备数量"); 39 40 //添加第5个单元格到第1行 41 TableCell cell5 = row1.AddCell(); 42 cell5.AddParagraph().AppendText("设备价格"); 43 44 45 //添加表格第2行 46 TableRow row2 = table.AddRow(true, false); 47 48 //添加第6个单元格到第2行 49 TableCell cell6 = row2.AddCell(); 50 cell6.AddParagraph().AppendText("1"); 51 52 //添加第7个单元格到第2行 53 TableCell cell7 = row2.AddCell(); 54 cell7.AddParagraph().AppendText("机床"); 55 56 //添加第8个单元格到第2行 57 TableCell cell8 = row2.AddCell(); 58 cell8.AddParagraph().AppendText("M170010"); 59 60 //添加第9个单元格到第2行 61 TableCell cell9 = row2.AddCell(); 62 cell9.AddParagraph().AppendText("12"); 63 64 //添加第10个单元格到第2行 65 TableCell cell10 = row2.AddCell(); 66 cell10.AddParagraph().AppendText("8W"); 67 table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow); 68 69 //保存文档 70 doc.SaveToFile("Table.docx"); 71 } 72 } 73 }
表格添加方法二:预定义表格行和列

1 using System; 2 using Spire.Doc; 3 using Spire.Doc.Fields; 4 using System.Drawing; 5 6 namespace CreateTable2_Word 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 //创建一个Document类实例,并添加section 13 Document document = new Document(); 14 Section section = document.AddSection(); 15 16 //添加表格指定表格的行数和列数(2行,5列) 17 Table table = section.AddTable(true); 18 table.ResetCells(2, 5); 19 20 //获取单元格(第1行第1个单元格)并添加文本内容,设置字体字号颜色等(单元格中内容及个性化设置可以根据需要来进行调整) 21 TextRange range = table[0, 0].AddParagraph().AppendText("序列号"); 22 range.CharacterFormat.FontName = "Arial"; 23 range.CharacterFormat.FontSize = 12; 24 range.CharacterFormat.TextColor = Color.Brown; 25 range.CharacterFormat.Bold = true; 26 27 //获取单元格(第1行第2个单元格)并添加文本 28 range = table[0, 1].AddParagraph().AppendText("设备名称"); 29 range.CharacterFormat.FontName = "Arial"; 30 range.CharacterFormat.FontSize = 12; 31 range.CharacterFormat.TextColor = Color.Brown; 32 range.CharacterFormat.Bold = true; 33 34 //获取单元格(第1行第3个单元格)并添加文本 35 range = table[0, 2].AddParagraph().AppendText("设备型号"); 36 range.CharacterFormat.FontName = "Arial"; 37 range.CharacterFormat.FontSize = 12; 38 range.CharacterFormat.TextColor = Color.Brown; 39 range.CharacterFormat.Bold = true; 40 41 //获取单元格(第1行第4个单元格)并添加文本 42 range = table[0, 3].AddParagraph().AppendText("设备数量"); 43 range.CharacterFormat.FontName = "Arial"; 44 range.CharacterFormat.FontSize = 12; 45 range.CharacterFormat.TextColor = Color.Brown; 46 range.CharacterFormat.Bold = true; 47 48 //获取单元格(第1行第5个单元格)并添加文本 49 range = table[0, 4].AddParagraph().AppendText("设备价格"); 50 range.CharacterFormat.FontName = "Arial"; 51 range.CharacterFormat.FontSize = 12; 52 range.CharacterFormat.TextColor = Color.Brown; 53 range.CharacterFormat.Bold = true; 54 55 //获取单元格(第2行第1个单元格)并添加文本 56 range = table[1, 0].AddParagraph().AppendText("1"); 57 range.CharacterFormat.FontName = "Arial"; 58 range.CharacterFormat.FontSize = 12; 59 60 //获取单元格(第2行第2个单元格)并添加文本 61 range = table[1, 1].AddParagraph().AppendText("机床"); 62 range.CharacterFormat.FontName = "Arial"; 63 range.CharacterFormat.FontSize = 12; 64 65 //获取单元格(第2行第3个单元格)并添加文本 66 range = table[1, 2].AddParagraph().AppendText("M170010"); 67 range.CharacterFormat.FontName = "Arial"; 68 range.CharacterFormat.FontSize = 12; 69 70 //获取单元格(第2行第4个单元格)并添加文本 71 range = table[1, 3].AddParagraph().AppendText("12"); 72 range.CharacterFormat.FontName = "Arial"; 73 range.CharacterFormat.FontSize = 12; 74 75 //获取单元格(第2行第5个单元格)并添加文本 76 range = table[1, 4].AddParagraph().AppendText("8W"); 77 range.CharacterFormat.FontName = "Arial"; 78 range.CharacterFormat.FontSize = 12; 79 80 //保存文档 81 document.SaveToFile("Table2.docx"); 82 } 83 } 84 }
以上两种方法中,鉴于文章篇幅,示例中只添加了比较简单的表格,在实际运用中,你可以根据自己的需要添加内容或者设置内容格式等。如果觉得对你有用的话,欢迎转载!
感谢阅读。
原文链接:https://www.cnblogs.com/Yesi/p/7838630.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数