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

慧都控件網

相關文章
相關標籤/搜索