2014-05-15 89 views
2

所以我目前正在嘗試將Word文檔(.DOC)轉換爲文本文件,因爲我想用它的正則表達式找東西的文檔中的文本文檔。所以我想出了下面的內容,它將Word文檔轉換爲富文本格式(通過將其附加到富文本框中),但這不會轉換爲純文本格式。當我用普通的文本文檔進行嘗試時,它會在新行上打印每個單詞。我一直無法找到有關如何在C#中執行此操作的任何信息。我使用的是C#和visual studio 2010.我不希望文檔中有任何特殊字符(如粗體,下劃線等),但是如果有人知道我如何能夠健壯並提取那些超級真棒。轉換爲Word文檔用C#

我想把它當作一個文本文檔,因爲有幾種方法,我知道我可以在普通文本中使用,但我懷疑他們會在字的文字工作,由於附帶的Word文檔隱藏/特殊字符。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.Office.Interop.Word; 

namespace ReadWordDocProject 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string testFile = @"C:\Users\<mycomputer>\Documents\TestItemHelpers\TestWordDoc.docx"; 

      Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
      Document document = application.Documents.Open(testFile);//path here 

      int count = document.Words.Count; 
      for (int i = 1; i <= count; i++) 
      { 
       string text = document.Words[i].Text; 
       //Do output with text here 
       richTextBox1.AppendText(text); 
      } 

      ((_Application)application).Quit(); //cast as _Application because there's ambiguity 
     } 


    } 
} 
+0

「當我與它印上了一個新行的每一個字普通的文本文檔試圖」什麼是你在這裏嘗試的代碼? –

+1

作爲一種非編程解決方案,您是否嘗試過從Word中複製整個文檔內容並將其粘貼到文本編輯器中?如果這只是一次性任務,那肯定是通向純文本文檔的最快途徑。 – adv12

+0

我會有很多像這樣的文件進來,這似乎有點不切實際。我知道該怎麼做,但我希望能有一個更簡單的解決方案。 – user3003304

回答

3

Microsoft表示您不應該使用Microsoft Office Interop在自動化應用程序中操作文檔。

您可以使用免費的圖書館像Spire Doc將Word文檔轉換爲TXT,然後打開txt文件。我認爲有一種方法可以直接從Spire保存到內存流中(我知道這裏有Aspose Words,但這不是免費的),但我不確定。

private void button1_Click(object sender, EventArgs e) 
{ 
    //Open word document 
    Document document = new Document(); 
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers"; 

    document.LoadFromFile(Path.Combine(docPath,"TestWordDoc.docx")); 

    //Save doc file. 
    document.SaveToFile(Path.Combine(docPath,"TestTxt.txt"), FileFormat.Txt); 

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt")); 

    //do regex here 

} 

編輯:如果你打算使用互操作,因爲它是好的,對用戶運行的活動(如在評論中指出),你可以將文檔保存爲文本文件,然後做正則表達式

private void button1_Click(object sender, EventArgs e) 
{ 
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers" 
    string testFile = "TestWordDoc.docx"; 


    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); 
    Document document = application.Documents.Open(Path.Combine(docPath,testFile); 

    application.ActiveDocument.SaveAs(Path.Combine(docPath,"TestTxt.txt"), WdSaveFormat.wdFormatText, ref noEncodingDialog); 
    ((_Application)application).Quit(); 

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt")); 

    //do regex here 


} 
+2

你的第一個鏈接只適用於*服務器端*處理。對於用戶運行的應用程序來說,這非常好。 – crashmstr

+0

我的程序可能會用於服務器端的工作,所以這對我來說可能確實很完美。 – user3003304

+0

我添加了Interop SaveAs,以防萬一您也有興趣查看。 – user1914368