温馨提示×

c# office怎样读取Word文档

c#
小樊
86
2024-07-26 18:29:12
栏目: 编程语言

要在C#中读取Word文档,可以使用Microsoft.Office.Interop.Word库。以下是一个简单的示例代码:

using Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
        Application application = new Application();
        Document document = application.Documents.Open("path_to_your_word_document.docx");

        string text = document.Content.Text;

        document.Close();
        application.Quit();

        System.Console.WriteLine(text);
    }
}

请注意,你需要安装Microsoft Office并在你的项目中引用Microsoft.Office.Interop.Word库才能运行此代码。你也可以使用第三方库如NPOI或Aspose来读取Word文档。

0