可以使用Microsoft.Office.Interop.Word库来读取Word文件中的内容,但是这个库只能在Windows系统上运行,而且需要安装Microsoft Office。

另一个选择是使用Open XML SDK,这个库可以在Windows、Linux和macOS上运行,而且不需要安装Microsoft Office。

下面是一个使用Open XML SDK读取Word文件中内容的示例代码:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;

public static string ReadWordFile(string filePath)
{
    using (WordprocessingDocument document = WordprocessingDocument.Open(filePath, false))
    {
        Body body = document.MainDocumentPart.Document.Body;
        return body.InnerText;
    }
}

这个方法会返回Word文件中的所有文本内容。如果需要进一步处理Word文件中的内容,可以使用Open XML SDK提供的其他API来获取更详细的信息。

NET core 读取Word文件中的内容

原文地址: https://www.cveoy.top/t/topic/bQsX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录