微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# 操作Word文本框——插入表格/读取表格/删除表格

在文本框中,我们可以操作很多元素,如文本、图片、表格等,在本篇文章中将着重介绍如何插入表格到文本框,插入的表格我们可以对表格进行格式化操作来丰富表格内容。此外,对于文本框中的表格内容,我们也可以根据需要来读取表格或者删除表格。

使用工具

示例代码

【示例1】插入表格到文本框

C#

using Spire.Doc;
 Spire.Doc.Documents; 
 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,1)">4实例化数组内容
            string[,] data = new [,]  
            {  
               {Name",1)">AgeGenderID },{John28Male0023Steve300024Lucy26female0025 }  
            };

            将数组内容添加到表格 
            for (int i = 0; i < 4; i++)
            {
                int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i,j].AddParagraph().AppendText(data[i,j]);
                    tableRange.CharacterFormat.FontName = ;
                }
            }

            应用表格样式
            table.ApplyStyle(DefaultTableStyle.MediumGrid3Accent1);

            保存并打开文档
            document.SavetoFile(Output.docx,FileFormat.Docx2013);
            System.Diagnostics.Process.Start();
        }
    }
}

这里应用表格格式,Spire.Doc 支持多种不同的表格类型,可根据需要自行选择。

表格添加效果

【示例2】读取文本框中的表格

C#

 

 Spire.Doc.Documents;
 Spire.Doc.Fields;
 System.IO;
 System.Text;


 GetTableFromTextBox_Doc
{
    [] args)
        {
            载入Word文档
            Document document = new Document(获取一个文本框
            TextBox textBox = document.TextBoxes[0];

            获取文本框中第一个表格
            Table table = textBox.Body.Tables[0] as Table;
            实例化StringBuilder类
            StringBuilder sb =  StringBuilder();

            遍历表格中的段落并提取文本
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell  row.Cells)
                {
                    foreach (Paragraph paragraph  cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteallText(text.txt

【示例3删除Word文本框中的表格

C#

 RemoveTableFormTextBox_Doc
{
    创建Document实例
            Document document = 删除文本框中第一个表格
            textBox.Body.Tables.RemoveAt(保存文档
            document.SavetoFile(RemoveTable.docx);
        }
    }
}

删除效果

附:

除了添加在文本框汇中操作表格以外,我们向文本框中添加图文混排的内容也是比较常见的,不仅仅只是添加简单文本或者图片,一些复杂的格式化的操作也是可以的,具体可以参阅博客C# 插入排版精良的Word文本框

 

以上是本次关于“C# 操作Word 文本框中的表格”的全部内容。如需转载,请注明出处!

感谢阅读!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐