C# 读取Word文本框中的文本、图片和表格(附VB.NET代码)

2021/7/20 22:06:27

本文主要是介绍C# 读取Word文本框中的文本、图片和表格(附VB.NET代码),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

【概述】

Word中可vb.net教程插入文c#教程本框,在文本框中可python基础教程添加文本、图片、表格等内容。本篇java基础教程文章通过C#程序代码介绍如何sql教程来读取文本框中的文本、图片和表格等内容。附VB.NET代码,有需要可作参考。

【程序环境】

程序中所需必要的程序集文件Spire.Doc.dll,及其他相关dll文件(见下文)。

用于测试的Word源文档如图:

【程序代码】

1.读取文本框中的文本

所需程序集:

【C#】

复制代码

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

namespace ExtractText
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word源文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取文本框
            TextBox textbox = doc.TextBoxes[0];

            //创建StringBuilder类的对象
            StringBuilder sb = new StringBuilder();

            //遍历文本框中的对象,获取文本
            foreach (object obj in textbox.Body.ChildObjects)
            {
                if (obj is Paragraph)
                {
                    String text = ((Paragraph)obj).Text;
                    sb.AppendLine(text);
                }
            }

            //保存写入的txt文档到指定路径
            File.WriteAllText("ExtractedText.txt", sb.ToString());
            System.Diagnostics.Process.Start("ExtractedText.txt");
        }
    }
}

复制代码

【vb.net】

复制代码

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

Namespace ExtractText
    Class Program
        Private Shared Sub Main(args As String())
            '加载Word源文档
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            '获取文本框
            Dim textbox As TextBox = doc.TextBoxes(0)

            '创建StringBuilder类的对象
            Dim sb As New StringBuilder()

            '遍历文本框中的对象,获取文本
            For Each obj As Object In textbox.Body.ChildObjects
                If TypeOf obj Is Paragraph Then
                    Dim text As [String] = DirectCast(obj, Paragraph).Text
                    sb.AppendLine(text)
                End If
            Next

            '保存写入的txt文档到指定路径
            File.WriteAllText("ExtractedText.txt", sb.ToString())
            System.Diagnostics.Process.Start("ExtractedText.txt")
        End Sub
    End Class
End Namespace

复制代码

文本读取结果:

2.读取文本框中的图片

所需程序集:

【C#】

复制代码

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;

namespace ExtractImg
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word源文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取文本框
            TextBox textbox = doc.TextBoxes[0];    

            int index = 0 ;
            //遍历文本框中所有段落
            for (int i = 0 ; i < textbox.Body.Paragraphs.Count;i++)
            {
                Paragraph paragraph = textbox.Body.Paragraphs[i];
                //遍历段落中的所有子对象
                for (int j = 0; j < paragraph.ChildObjects.Count; j++)
                {
                    object obj = paragraph.ChildObjects[j];
                    
                    //判定对象是否为图片
                    if (obj is DocPicture)
                    {
                        //获取图片
                        DocPicture picture = (DocPicture) obj;
                        String imageName = String.Format("Image-{0}.png", index);
                        picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
                        index++;
                    }
                }
            }
                 
        }
    }
}

复制代码

【vb.net】

复制代码

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace ExtractImg
    Class Program
        Private Shared Sub Main(args As String())
            '加载Word源文档
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            '获取文本框
            Dim textbox As TextBox = doc.TextBoxes(0)

            Dim index As Integer = 0
            '遍历文本框中所有段落
            For i As Integer = 0 To textbox.Body.Paragraphs.Count - 1
                Dim paragraph As Paragraph = textbox.Body.Paragraphs(i)
                '遍历段落中的所有子对象
                For j As Integer = 0 To paragraph.ChildObjects.Count - 1
                    Dim obj As Object = paragraph.ChildObjects(j)

                    '判定对象是否为图片
                    If TypeOf obj Is DocPicture Then
                        '获取图片
                        Dim picture As DocPicture = DirectCast(obj, DocPicture)
                        Dim imageName As [String] = [String].Format("Image-{0}.png", index)
                        picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
                        index += 1
                    End If
                Next
            Next

        End Sub
    End Class
End Namespace

复制代码

图片读取结果:

3.读取文本框中的表格

所需程序集:

【C#】

复制代码

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

namespace ExtractTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //获取文本框
            TextBox textbox = doc.TextBoxes[0];

            //获取文本框中表格
            Table table = textbox.Body.Tables[0] as Table;

            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("ExtractedTable.txt", sb.ToString());
        }
    }
}

复制代码

【vb.net】

复制代码

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

Namespace ExtractTable
    Class Program
        Private Shared Sub Main(args As String())
            '加载Word文档
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            '获取文本框
            Dim textbox As TextBox = doc.TextBoxes(0)

            '获取文本框中表格
            Dim table As Table = TryCast(textbox.Body.Tables(0), Table)

            Dim sb As New StringBuilder()

            '遍历表格中的段落并提取文本
            For Each row As TableRow In table.Rows
                For Each cell As TableCell In row.Cells
                    For Each paragraph As Paragraph In cell.Paragraphs
                        sb.AppendLine(paragraph.Text)
                    Next
                Next
            Next
            File.WriteAllText("ExtractedTable.txt", sb.ToString())
        End Sub
    End Class
End Namespace

复制代码

表格数据读取结果:



这篇关于C# 读取Word文本框中的文本、图片和表格(附VB.NET代码)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程