当前位置:网站首页>C#/VB.NET Word转Text
C#/VB.NET Word转Text
2022-06-23 21:49:00 【51CTO】
相信大家经常会在Word文档中编写文章,但是有时候我们需要将Word文档转换成txt格式。而txt格式相较于其他格式来说,
- 体积小、存储简单方便
- txt格式比较简单,不会中毒
- txt格式是电脑和很多移动设备的通用格式
今天这篇文章将以C#/VB.NET代码为例,向大家详细介绍如何将Word转成txt格式。详情请阅读以下内容。
程序环境:
本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用Spire.Doc.dll文件:
方法1:将 Free Spire.Doc for .NET 下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
方法2:通过 NuGet 安装。可通过以下2种方法安装:
(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
(2)将以下内容复制到PM控制台安装。
Install-Package FreeSpire.Doc -Version 10.2.0
将Word转为Text
- 创建一个Document实例。
- 使用 Document.LoadFromFile() 方法加载 Word 示例文档。
- 使用 Document.SaveToFile() 方法将文档保存为 TXT 文件。
[C#]
using
System.
Data;
using
System.
Drawing;
using
System.
Text;
using
Spire.
Doc;
using
Spire.
Doc.
Documents;
namespace
ConsoleApplication2
{
class
Program
{
static
void
Main(
string[]
args)
{
//新建一个文档.
Document
document
=
new
Document();
//加载实例文档.
document.
LoadFromFile(
"NewSample1.docx");
string
result
=
"Result-WordToText.txt";
//保存文档.
document.
SaveToFile(
result,
FileFormat.
Txt);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
【VB.NET】
Imports
System
.Data
Imports
System
.Drawing
Imports
System
.Text
Imports
Spire
.Doc
Imports
Spire
.Doc
.Documents
Namespace
ConsoleApplication2
Class
Program
Private
Shared
Sub
Main(
ByVal
args()
As
String)
'新建一个文档.
Dim
document
As
Document =
New
Document
'加载实例文档.
document
.LoadFromFile(
"NewSample1.docx")
Dim
result
As
String =
"Result-WordToText.txt"
'保存文档.
document
.SaveToFile(
result,
FileFormat
.Txt)
End
Sub
End
Class
End
Namespace
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
效果图:

注意:代码中生成的文档路径为的VS程序的Debug路径,文件路径也可自定义为其他路径。
边栏推荐
- H265 enables mobile phone screen projection
- This article takes you to understand the JVM class loading mechanism
- Activity creation process details
- Overall solution for digital transformation of medical supply chain management
- VNC multi gear resolution adjustment, 2008R2 setting 1280 × 1024 resolution
- Problem solving: inittramfs unpacking failed:decoding failed
- Why don't people like PHP?
- Method of thread synchronization in kotlin
- Three ways to enable IPv6 on Tencent cloud
- How to lossless publish API gateway why do you need API gateway?
猜你喜欢

在宇宙的眼眸下,如何正确地关心东数西算?
![[technical dry goods] the technical construction route and characteristics of zero trust in ant Office](/img/d1/ce999b9f72bbb8f692c4298b4042aa.png)
[technical dry goods] the technical construction route and characteristics of zero trust in ant Office

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

Save: software analysis, verification and test platform

蚂蚁集团自研TEE技术通过国家级金融科技产品认证

游戏安全丨喊话CALL分析-写代码

SLSA: 成功SBOM的促进剂

Chaos engineering, learn about it

解密抖音春节红包背后的技术设计与实践

【技术干货】蚂蚁办公零信任的技术建设路线与特点
随机推荐
The article "essence" introduces you to VMware vSphere network, vswitch and port group!
openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区
Start learning simple JS
SAVE: 软件分析验证和测试平台
How to transfer files from the local fortress server
Source code analysis of jmeter5.1 core class testcompiler
[tcapulusdb knowledge base] insert data example (TDR table)
What is the API gateway architecture? What are the common gateway types?
Performance test - LoadRunner obtains the return value and user-defined parameters (parameter operation)
Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers
AAAI 2022 | Tencent Youtu 14 papers were selected, including image coloring, face security, scene text recognition and other frontier fields
Pourquoi une seule valeur apparaît - elle sur votre carte de données?
What are the application flow restrictions of API gateway framework?
Go build command (go language compilation command) complete introduction
How to batch output EAN13 code to PDF
How to build a website after registering a domain name
Ant group's self-developed tee technology has passed the national financial technology product certification
How does data Vientiane CI | app quickly integrate HLS encryption to prevent video leakage?
Beauty of script │ VBS introduction interactive practice
Slsa: accelerator for successful SBOM