Class ExcelUtil

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

public class ExcelUtil extends Object
Excel 读写工具类(xlsx/xls),参考 Hutool 的 ExcelUtil 设计。

读取时单元格类型感知(数字/日期/布尔/字符串),写入时按 Java 类型自动落格; Bean 写入以字段名作为表头。

Since:
0.1.0
Author:
suretool
  • Method Details

    • read

      public static List<List<Object>> read(File file) throws IOException
      读取 Excel 第一个工作表(每行为单元格值列表)。
      Parameters:
      file - Excel 文件
      Returns:
      行列表
      Throws:
      IOException - 读取失败
    • read

      public static List<List<Object>> read(File file, int sheetIndex) throws IOException
      读取指定工作表。
      Parameters:
      file - Excel 文件
      sheetIndex - 工作表索引
      Returns:
      行列表
      Throws:
      IOException - 读取失败
    • readWithHeader

      public static List<Map<String,Object>> readWithHeader(File file) throws IOException
      读取第一个工作表,第一行作为表头,返回 Map 列表。
      Parameters:
      file - Excel 文件
      Returns:
      Map 列表
      Throws:
      IOException - 读取失败
    • readWithHeader

      public static List<Map<String,Object>> readWithHeader(File file, int sheetIndex) throws IOException
      读取指定工作表,第一行作为表头,返回 Map 列表。
      Parameters:
      file - Excel 文件
      sheetIndex - 工作表索引
      Returns:
      Map 列表
      Throws:
      IOException - 读取失败
    • readSheetNames

      public static List<String> readSheetNames(File file) throws IOException
      工作表名称列表。
      Parameters:
      file - Excel 文件
      Returns:
      名称列表
      Throws:
      IOException - 读取失败
    • write

      public static File write(File file, List<List<Object>> rows) throws IOException
      写入行数据到默认工作表。
      Parameters:
      file - 目标文件
      rows - 行列表(每行为值列表)
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • write

      public static File write(File file, String sheetName, List<List<Object>> rows) throws IOException
      写入行数据到指定工作表。
      Parameters:
      file - 目标文件
      sheetName - 工作表名
      rows - 行列表(每行为值列表)
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • writeBeans

      public static File writeBeans(File file, List<?> beans) throws IOException
      Bean 列表写入 Excel(字段名作为表头)。
      Parameters:
      file - 目标文件
      beans - Bean 列表
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • writeBeans

      public static File writeBeans(File file, String sheetName, List<?> beans) throws IOException
      Bean 列表写入 Excel(字段名作为表头)。
      Parameters:
      file - 目标文件
      sheetName - 工作表名
      beans - Bean 列表
      Returns:
      目标文件
      Throws:
      IOException - 写入失败
    • read

      public static List<List<Object>> read(File file, int sheetIndex, int startRow) throws IOException
      从指定行(含)开始读取工作表。
      Parameters:
      file - Excel 文件
      sheetIndex - 工作表索引
      startRow - 起始行号(0 起)
      Returns:
      行数据列表
      Throws:
      IOException - IO 异常
    • writeBeans

      public static File writeBeans(File file, String sheetName, List<?> beans, String... headers) throws IOException
      写入 Bean 列表,可指定表头(表头同时作为字段选择器)。
      Parameters:
      file - Excel 文件
      sheetName - 工作表名
      beans - Bean 列表
      headers - 表头字段名(按需选择列)
      Returns:
      文件
      Throws:
      IOException - IO 异常