-
C# 操作Word文本框——插入表格/读取表格/删除表格
在文本框中,我们可以操作很多元素,如文本、图片、表格等,在本篇文章中将着重介绍如何插入表格到文本框,插入的表格我们可以对表格进行格式化操作来丰富表格内容。此外,对于文本框中的表格内容,我们也可以根据需要来读取表格或者删除表格。
使用工具
- Free Spire.Doc for .NET 6.3 https://www.e-iceblue.cn/Introduce/Free-Spire-Doc-NET.html(免费版)
示例代码
【示例1】插入表格到文本框
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace InsertTableToTextbox_Doc { class Program { static void Main(string[] args) { //创建一个Document类对象 Document document = new Document(); //添加section到文档 Section section = document.AddSection(); //添加段落section Paragraph paragraph = section.AddParagraph(); //添加指定大小的文本框到段落 TextBox textbox = paragraph.AppendTextBox(300, 100); //添加文本到文本,设置文本格式 Paragraph textboxParagraph = textbox.Body.AddParagraph(); TextRange textboxRange = textboxParagraph.AppendText("Sample Report 1"); textboxRange.CharacterFormat.FontName = "Arial"; //插入表格到文本框 Table table = textbox.Body.AddTable(true); //指定表格行数、列数 table.ResetCells(4, 4); //实例化数组内容 string[,] data = new string[,] { {"Name","Age","Gender","ID" }, {"John","28","Male","0023" }, {"Steve","30","Male","0024" }, {"Lucy","26","female","0025" } }; //将数组内容添加到表格 for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]); tableRange.CharacterFormat.FontName = "Arial"; } } //应用表格样式 table.ApplyStyle(DefaultTableStyle.MediumGrid3Accent1); //保存并打开文档 document.SaveToFile("Output.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("Output.docx"); } } }
这里应用表格格式,Spire.Doc 支持多种不同的表格类型,可根据需要自行选择。
表格添加效果:
【示例2】读取文本框中的表格
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.IO; using System.Text; namespace GetTableFromTextbox_Doc { class Program { static void Main(string[] args) { //载入Word文档 Document document = new Document("Output.docx"); //获取第一个文本框 TextBox textbox = document.TextBoxes[0]; //获取文本框中第一个表格 Table table = textbox.Body.Tables[0] as Table; //实例化StringBuilder类 StringBuilder sb = new StringBuilder(); //遍历表格中的段落并提取文本 foreach (TableRow row in table.Rows) { foreach (TableCell cell in row.Cells) { foreach (Paragraph paragraph in cell.Paragraphs) { sb.AppendLine(paragraph.Text); } } } File.WriteAllText("text.txt", sb.ToString()); } } }
读取结果:
【示例3】删除Word文本框中的表格
C#
using Spire.Doc; using Spire.Doc.Fields; namespace RemoveTableFormTextbox_Doc { class Program { static void Main(string[] args) { //创建Document实例 Document document = new Document("Output.docx"); //获取第一个文本框 TextBox textbox = document.TextBoxes[0]; //删除文本框中第一个表格 textbox.Body.Tables.RemoveAt(0); //保存文档 document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("RemoveTable.docx"); } } }
删除效果:
附:
除了添加在文本框汇中操作表格以外,我们向文本框中添加图文混排的内容也是比较常见的,不仅仅只是添加简单文本或者图片,一些复杂的格式化的操作也是可以的,具体可以参阅博客“C# 插入排版精良的Word文本框”
以上是本次关于“C# 操作Word 文本框中的表格”的全部内容。如需转载,请注明出处!
原文链接:https://www.cnblogs.com/Yesi/p/9072177.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
如何完美解决前端数字计算精度丢失与数