Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

net core Aspose.Words.Document 读取Word文件中的文字

  1. User

    net core Aspose.Words.Document 读取Word文件中的文字

  2. Assistant

    在 .NET Core 中使用 Aspose.Words 可以轻松读取 Word 文件中的文字,可以按照以下步骤进行操作:

    1. 安装 Aspose.Words NuGet 包。可以在 Visual Studio 中使用 NuGet 管理器来安装该包,或者执行以下命令:

      dotnet add package Aspose.Words
      
    2. 使用 Aspose.Words 的 Document 类打开 Word 文件。可以使用本地文件路径或字节数组,如下所示:

      using Aspose.Words;
      
      // 打开本地文件
      Document doc = new Document("path/to/document.docx");
      
      // 打开字节数组
      byte[] data = File.ReadAllBytes("path/to/document.docx");
      Document doc = new Document(new MemoryStream(data));
      
    3. 使用 Document 类的 Text 属性获取全部文本内容,或者使用 Document 类的 GetText 方法获取仅包含纯文本内容的字符串,如下所示:

      // 获取全部文本内容
      string text = doc.Text;
      
      // 获取仅包含纯文本内容的字符串
      string text = doc.GetText();
      
    4. 如果需要对 Word 文件中的文本进行更复杂的处理,还可以使用 Aspose.Words 提供的其他方法和属性来获取或修改 Word 文档中的内容。编辑完成后,可以使用 Document 类的 Save 方法将修改后的文档保存回 Word 文件或其他格式。

    更多关于 Aspose.Words 的使用,请参阅官方文档。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.