Spire.Doc 教程:如何将Word 转换为文本

来源:互联网 发布:制作婚庆视频软件 编辑:程序博客网 时间:2024/05/16 02:49

为什么我们要将Word转换为文本?

文本格式通常包含纯文本或最小格式。 我们可以从Word到Text转换中受益以下几点:

  • 减小文件大小以节省磁盘空间
  • 兼容几乎所有的应用程序
  • 没有最小的格式或不正常的字符

如何将Word转换为文本?

Spire.Doc是一个MS Word 组件,使用户可以直接执行各种Word文档处理任务,例如为.NET和Silverlight生成,读取,写入和修改Word文档。 它支持将文档从Word文档转换为文本,HTML,PDF,XML,RTF,Docx,Dot等。

使用.NET Framework 2.0(或以上)下载Spire.Doc(或Spire.Office),并按照以下简单代码将Word转换为文本。

Step 1:在Visual Studio中创建一个项目,添加Spire.Doc DLL作为参考。

Step 2:使用以下代码加载要转换为Txt文件的本地Word文档。

  Document document = new Document();  document.LoadFromFile(@"D:\Work\Stephen\2011.12.05\Sample.doc");

Step 3:使用以下代码将文本doc文件另存为HTML

  document.SaveToFile("Sample.txt", FileFormat.Txt);

Step 4:将以下完整代码写入项目,然后按F5启动,将自动生成一个Txt文件。

[C#]

using System;using System.Windows.Forms;using Spire.Doc;using Spire.Doc.Documents;namespace tohtml_3{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            //Create word document            Document document = new Document();            document.LoadFromFile(@"D:\Work\Stephen\2011.12.05\Sample.doc");            //Save doc file.            document.SaveToFile("Sample.txt", FileFormat.Txt);            //Launching the MS Word file.            WordDocViewer("Sample.txt");        }        private void WordDocViewer(string fileName)        {            try            {                System.Diagnostics.Process.Start(fileName);            }            catch { }        }    }

[VB.NET]

Imports SystemImports System.Windows.FormsImports Spire.DocImports Spire.Doc.DocumentsNamespace tohtml_3    Partial Public Class Form1        Inherits Form        Public Sub New()            InitializeComponent()        End Sub        Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)            'Create word document            Dim document As New Document()            document.LoadFromFile("D:\Work\Stephen\2011.12.05\Sample.doc")            'Save doc file.            document.SaveToFile("Sample.txt", FileFormat.Txt)            'Launching the MS Word file.            WordDocViewer("Sample.txt")        End Sub        Private Sub WordDocViewer(ByVal fileName As String)            Try                System.Diagnostics.Process.Start(fileName)            Catch            End Try        End Sub    End ClassEnd Namespace

效果截图

图片1

慧都控件网

阅读全文
0 0
原创粉丝点击