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

为何咱们要将Word转换为文本?html

文本格式一般包含纯文本或最小格式。 咱们能够从Word到Text转换中受益如下几点:spa

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

如何将Word转换为文本?orm

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

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

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文件另存为HTMLget

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

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

[C#]it

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 System
Imports System.Windows.Forms
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace 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 Class
End Namespace

效果截图

图片1

慧都控件网

相关文章
相关标签/搜索