所以我目前正在嘗試將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
}
}
}
「當我與它印上了一個新行的每一個字普通的文本文檔試圖」什麼是你在這裏嘗試的代碼? –
作爲一種非編程解決方案,您是否嘗試過從Word中複製整個文檔內容並將其粘貼到文本編輯器中?如果這只是一次性任務,那肯定是通向純文本文檔的最快途徑。 – adv12
我會有很多像這樣的文件進來,這似乎有點不切實際。我知道該怎麼做,但我希望能有一個更簡單的解決方案。 – user3003304