2014-05-15 88 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