Class WordUtil

java.lang.Object
com.sure.tool.poi.WordUtil

public class WordUtil extends Object
Word(docx)读写工具类,参考 Hutool 的 WordUtil 设计。

读取支持段落与表格文本提取;写入支持单段/多段简单文档。

Since:
0.1.0
Author:
suretool
  • Method Details

    • readText

      public static String readText(File file) throws IOException
      读取 docx 全文(段落 + 表格文本)。
      Parameters:
      file - docx 文件
      Returns:
      全文文本
      Throws:
      IOException - 读取失败
    • readParagraphs

      public static List<String> readParagraphs(File file) throws IOException
      读取 docx 段落文本。
      Parameters:
      file - docx 文件
      Returns:
      段落列表
      Throws:
      IOException - 读取失败
    • readTables

      public static List<List<List<String>>> readTables(File file) throws IOException
      读取 docx 所有表格(表格 → 行 → 单元格)。
      Parameters:
      file - docx 文件
      Returns:
      表格列表
      Throws:
      IOException - 读取失败
    • write

      public static File write(File file, String text) throws IOException
      写入简单 docx(单段落)。
      Parameters:
      file - 目标文件
      text - 文本
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • write

      public static File write(File file, List<String> paragraphs) throws IOException
      写入 docx(多段落)。
      Parameters:
      file - 目标文件
      paragraphs - 段落列表
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • write

      public static File write(File file, String text, boolean append) throws IOException
      写入文本;append 为 true 且文件存在时追加段落。
      Parameters:
      file - 文档文件
      text - 文本
      append - 是否追加
      Returns:
      文件
      Throws:
      IOException - IO 异常