热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

【概述】Word中可vb.net教程插入文c#教程本框,在文本框中可python基础教程添加文本、图片、表格等内容。本篇java基础教程文章通过C#程序代码介绍如何sql教程来读取


【概述】

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 {
Paragraph paragraph = textbox.Body.Paragraphs[i];
//遍历段落中的所有子对象
for (int j = 0; 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

表格数据读取结果:



推荐阅读
author-avatar
温恭凯364092
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有