在 C# 中将 Microsoft Word 文档转换为文本文件






4.69/5 (7投票s)
本技巧解释了如何在 C# 中将 Microsoft Word 文档转换为文本文件,使用 Microsoft Word 对象库。
引言
在本技巧中,我将解释如何在 C# 中将 Microsoft Word 文档转换为文本文件。为此,必须安装 Word。
添加对 Microsoft Word 对象库的引用
第一步是添加对 Microsoft Word 对象库的引用。在 Visual Studio 中,选择“添加引用...”,转到“COM”,然后选择“Microsoft Word [此处为版本号] 对象库”。
如您在图像中看到的,我使用的是 Microsoft Word 15.0 对象库,这是 Word 2013 的库。您的编号可能不是 15.0。
代码
在代码文件的顶部,我们将添加以下 using [命名空间]
语句
using System.IO;
using Word = Microsoft.Office.Interop.Word;
现在,我们可以直接编写 Word.Document
而不是例如 Microsoft.Office.Interop.Word.Document
。现在,我们将使用以下代码询问用户他/她想要转换的文件
Console.WriteLine("Please enter the full file path of your Word document (without quotes):");
object path = Console.ReadLine();
Console.WriteLine("Please enter the file path of the text document in which you want to store the text of your word document (without quotes):");
string txtPath = Console.ReadLine();
如代码所示,对于 Word 文档的路径,需要完整的路径。如果您只写 test.docx
,那么您实际上会尝试转换 C:\Windows\system32\test.docx
而不是转换器文件夹中的 test.docx
文件。对于文本文件的文件路径,可以写 test.txt
,因为这将在转换器的文件夹中创建 test.txt
文件。Word 文件路径也必须是 object
,而不是 string
,因为当我们打开 Word 文件时,参数应该是 object
。现在,我们将打开 Word 文件并使用以下代码检索文本
Word.Application app = new Word.Application();
Word.Document doc;
object missing = Type.Missing;
object readOnly = true;
try
{
doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string text = doc.Content.Text;
File.WriteAllText(txtPath, text);
Console.WriteLine("Converted!");
}
在这里,我们创建一个打开文档的 Word Application
。Open
方法的第一个参数是文件路径,第三个参数是是否以只读方式打开文件(在本例中为是)。文本存储在 Content.Text
中,然后我们使用 File.WriteAllText
方法将文本写入文件。现在,我们将创建 catch
和 finally
块
catch
{
Console.WriteLine("An error occured. Please check the file path to your word document, and whether the word document is valid.");
}
finally
{
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
app.Quit(ref saveChanges, ref missing, ref missing);
}
因为我们不想保存更改(甚至没有进行更改),所以我们使用 WdSaveOptions.wdDoNotSaveChanges
。Application.Quit
方法关闭所有打开的文档,并退出 Word 应用程序。如果我们将所有代码片段合并,我们将得到
Console.WriteLine("Please enter the full file path of your Word document (without quotes):");
object path = Console.ReadLine();
Console.WriteLine("Please enter the file path of the text document in which you want to store the text of your word document (without quotes):");
string txtPath = Console.ReadLine();
Word.Application app = new Word.Application();
Word.Document doc;
object missing = Type.Missing;
object readOnly = true;
try
{
doc = app.Documents.Open(ref path, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
string text = doc.Content.Text;
File.WriteAllText(txtPath, text);
Console.WriteLine("Converted!");
}
catch
{
Console.WriteLine("An error occured. Please check the file path to your word document, and whether the word document is valid.");
}
finally
{
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
app.Quit(ref saveChanges, ref missing, ref missing);
}
历史
- 2014 年 1 月 5 日:第一个版本