Class CellKit

java.lang.Object
org.miaixz.bus.office.excel.xyz.CellKit

public class CellKit extends Object
Excel表格中单元格工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    根据表元的列名转换为列号
    static org.apache.poi.ss.usermodel.Cell
    getCell(org.apache.poi.ss.usermodel.Row row, int cellIndex)
    获取单元格,如果单元格不存在,返回NullCell
    static org.apache.poi.ss.usermodel.Cell
    getCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y, boolean isCreateIfNotExist)
    获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null
    static org.apache.poi.ss.util.CellRangeAddress
    getCellRangeAddress(org.apache.poi.ss.usermodel.Cell cell)
    获取合并单元格CellRangeAddress,如果不是返回null
    static org.apache.poi.ss.util.CellRangeAddress
    getCellRangeAddress(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
    获取合并单元格CellRangeAddress,如果不是返回null
    static org.apache.poi.ss.util.CellRangeAddress
    getCellRangeAddress(org.apache.poi.ss.usermodel.Sheet sheet, String locationRef)
    获取合并单元格CellRangeAddress,如果不是返回null
    static Object
    getCellValue(org.apache.poi.ss.usermodel.Cell cell)
    获取单元格值
    static Object
    getCellValue(org.apache.poi.ss.usermodel.Cell cell, boolean isTrimCellValue)
    获取单元格值
    static Object
    getCellValue(org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.ss.usermodel.CellType cellType, boolean isTrimCellValue)
    获取单元格值
    static Object
    getCellValue(org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.ss.usermodel.CellType cellType, CellEditor cellEditor)
    获取单元格值 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型
    static Object
    getCellValue(org.apache.poi.ss.usermodel.Cell cell, CellEditor cellEditor)
    获取单元格值
    static org.apache.poi.ss.usermodel.Cell
    getFirstCellOfMerged(org.apache.poi.ss.usermodel.Cell cell)
    获取合并单元格中的第一个单元格 传入的cell可以是合并单元格范围内的任意一个单元格
    static MergedCell
    getMergedCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
    获取合并单元格 传入的x,y坐标(列行数)可以是合并单元格范围内的任意一个单元格
    static org.apache.poi.ss.usermodel.Cell
    getOrCreateCell(org.apache.poi.ss.usermodel.Row row, int cellIndex)
    获取已有单元格或创建新单元格
    static org.apache.poi.ss.usermodel.Cell
    getOrCreateCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
    获取或创建指定坐标单元格
    static String
    indexToColName(int index)
    将Sheet列号变为列名
    static boolean
    isMergedRegion(org.apache.poi.ss.usermodel.Cell cell)
    判断指定的单元格是否是合并单元格
    static boolean
    isMergedRegion(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
    判断指定的单元格是否是合并单元格
    static boolean
    isMergedRegion(org.apache.poi.ss.usermodel.Sheet sheet, String locationRef)
    判断指定的单元格是否是合并单元格
    static int
    mergingCells(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.util.CellRangeAddress cellRangeAddress)
    合并单元格,可以根据设置的值来合并行和列
    static int
    mergingCells(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.util.CellRangeAddress cellRangeAddress, org.apache.poi.ss.usermodel.CellStyle cellStyle)
    合并单元格,可以根据设置的值来合并行和列
    static org.apache.poi.ss.util.CellRangeAddress
    of(int firstRow, int lastRow, int firstCol, int lastCol)
    根据给定的行和列索引创建一个CellRangeAddress对象 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
    static org.apache.poi.ss.util.CellRangeAddress
    of(String ref)
    根据字符串表示法创建一个CellRangeAddress对象 该方法提供了一种通过直接使用字符串引用来创建CellRangeAddress对象的便捷方式 字符串引用应符合Excel中单元格范围的常规表示法,如"B1" 或 "A1:B2"。
    static org.apache.poi.ss.util.CellRangeAddress
    ofSingleColumn(int lastRow, int col)
    根据给定的行和列索引创建一个单列CellRangeAddress对象,从首行开始。
    static org.apache.poi.ss.util.CellRangeAddress
    ofSingleColumn(int firstRow, int lastRow, int col)
    根据给定的行和列索引创建一个单列CellRangeAddress对象。
    static org.apache.poi.ss.util.CellRangeAddress
    ofSingleRow(int row, int lastCol)
    根据给定的行和列索引创建一个单行的CellRangeAddress对象,从首列开始。
    static org.apache.poi.ss.util.CellRangeAddress
    ofSingleRow(int row, int firstCol, int lastCol)
    根据给定的行和列索引创建一个单行的CellRangeAddress对象 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
    static void
    remove(org.apache.poi.ss.usermodel.Cell cell)
    移除指定单元格
    static void
    setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value)
    设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
    static void
    setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, org.apache.poi.ss.usermodel.CellStyle style, CellEditor cellEditor)
    设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
    static void
    setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, CellEditor cellEditor)
    设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
    static void
    setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, StyleSet styleSet, boolean isHeader, CellEditor cellEditor)
    设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
    static void
    setComment(org.apache.poi.ss.usermodel.Cell cell, String commentText, String commentAuthor)
    为特定单元格添加批注
    static void
    setComment(org.apache.poi.ss.usermodel.Cell cell, String commentText, String commentAuthor, org.apache.poi.ss.usermodel.ClientAnchor anchor)
    为特定单元格添加批注
    static org.apache.poi.ss.util.CellReference
    toCellReference(String locationRef)
    将Excel中地址标识符(例如A11,B5)等转换为行列表示 例如:A11 - col:0,row:10,B5-col:1,row:4

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CellKit

      public CellKit()
  • Method Details

    • getCellValue

      public static Object getCellValue(org.apache.poi.ss.usermodel.Cell cell)
      获取单元格值
      Parameters:
      cell - Cell单元格
      Returns:
      值,类型可能为:Date、Double、Boolean、String
    • getCellValue

      public static Object getCellValue(org.apache.poi.ss.usermodel.Cell cell, boolean isTrimCellValue)
      获取单元格值
      Parameters:
      cell - Cell单元格
      isTrimCellValue - 如果单元格类型为字符串,是否去掉两边空白符
      Returns:
      值,类型可能为:Date、Double、Boolean、String
    • getCellValue

      public static Object getCellValue(org.apache.poi.ss.usermodel.Cell cell, CellEditor cellEditor)
      获取单元格值
      Parameters:
      cell - Cell单元格
      cellEditor - 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
      Returns:
      值,类型可能为:Date、Double、Boolean、String
    • getCellValue

      public static Object getCellValue(org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.ss.usermodel.CellType cellType, boolean isTrimCellValue)
      获取单元格值
      Parameters:
      cell - Cell单元格
      cellType - 单元格值类型CellType枚举
      isTrimCellValue - 如果单元格类型为字符串,是否去掉两边空白符
      Returns:
      值,类型可能为:Date、Double、Boolean、String
    • getCellValue

      public static Object getCellValue(org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.ss.usermodel.CellType cellType, CellEditor cellEditor)
      获取单元格值 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型
      Parameters:
      cell - Cell单元格
      cellType - 单元格值类型CellType枚举,如果为null默认使用cell的类型
      cellEditor - 单元格值编辑器。可以通过此编辑器对单元格值做自定义操作
      Returns:
      值,类型可能为:Date、Double、Boolean、String
    • setCellValue

      public static void setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, StyleSet styleSet, boolean isHeader, CellEditor cellEditor)
      设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
      Parameters:
      cell - 单元格
      value - 值
      styleSet - 单元格样式集,包括日期等样式,null表示无样式
      isHeader - 是否为标题单元格
      cellEditor - 单元格值编辑器,可修改单元格值或修改单元格,null表示不编辑
    • setCellValue

      public static void setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, org.apache.poi.ss.usermodel.CellStyle style, CellEditor cellEditor)
      设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
      Parameters:
      cell - 单元格
      value - 值
      style - 自定义样式,null表示无样式
      cellEditor - 单元格值编辑器,可修改单元格值或修改单元格,null表示不编辑
    • setCellValue

      public static void setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value, CellEditor cellEditor)
      设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
      Parameters:
      cell - 单元格
      value - 值或CellSetter
      cellEditor - 单元格值编辑器,可修改单元格值或修改单元格,null表示不编辑
    • setCellValue

      public static void setCellValue(org.apache.poi.ss.usermodel.Cell cell, Object value)
      设置单元格值 根据传入的styleSet自动匹配样式 当为头部样式时默认赋值头部样式,但是头部中如果有数字、日期等类型,将按照数字、日期样式设置
      Parameters:
      cell - 单元格
      value - 值或CellSetter
    • getOrCreateCell

      public static org.apache.poi.ss.usermodel.Cell getOrCreateCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
      获取或创建指定坐标单元格
      Parameters:
      sheet - Sheet
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      Returns:
      Cell
    • getCell

      public static org.apache.poi.ss.usermodel.Cell getCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y, boolean isCreateIfNotExist)
      获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null
      Parameters:
      sheet - Sheet
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      isCreateIfNotExist - 单元格不存在时是否创建
      Returns:
      Cell
    • getCell

      public static org.apache.poi.ss.usermodel.Cell getCell(org.apache.poi.ss.usermodel.Row row, int cellIndex)
      获取单元格,如果单元格不存在,返回NullCell
      Parameters:
      row - Excel表的行
      cellIndex - 列号
      Returns:
      Row
    • getOrCreateCell

      public static org.apache.poi.ss.usermodel.Cell getOrCreateCell(org.apache.poi.ss.usermodel.Row row, int cellIndex)
      获取已有单元格或创建新单元格
      Parameters:
      row - Excel表的行
      cellIndex - 列号
      Returns:
      Row
    • isMergedRegion

      public static boolean isMergedRegion(org.apache.poi.ss.usermodel.Sheet sheet, String locationRef)
      判断指定的单元格是否是合并单元格
      Parameters:
      sheet - Sheet
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      是否是合并单元格
    • isMergedRegion

      public static boolean isMergedRegion(org.apache.poi.ss.usermodel.Cell cell)
      判断指定的单元格是否是合并单元格
      Parameters:
      cell - Cell
      Returns:
      是否是合并单元格
    • isMergedRegion

      public static boolean isMergedRegion(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
      判断指定的单元格是否是合并单元格
      Parameters:
      sheet - Sheet
      x - 列号,从0开始
      y - 行号,从0开始
      Returns:
      是否是合并单元格
    • mergingCells

      public static int mergingCells(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.util.CellRangeAddress cellRangeAddress)
      合并单元格,可以根据设置的值来合并行和列
      Parameters:
      sheet - 表对象
      cellRangeAddress - 合并单元格范围,定义了起始行列和结束行列
      Returns:
      合并后的单元格号
    • mergingCells

      public static int mergingCells(org.apache.poi.ss.usermodel.Sheet sheet, org.apache.poi.ss.util.CellRangeAddress cellRangeAddress, org.apache.poi.ss.usermodel.CellStyle cellStyle)
      合并单元格,可以根据设置的值来合并行和列
      Parameters:
      sheet - 表对象
      cellRangeAddress - 合并单元格范围,定义了起始行列和结束行列
      cellStyle - 单元格样式,只提取边框样式,null表示无样式
      Returns:
      合并后的单元格号
    • getFirstCellOfMerged

      public static org.apache.poi.ss.usermodel.Cell getFirstCellOfMerged(org.apache.poi.ss.usermodel.Cell cell)
      获取合并单元格中的第一个单元格 传入的cell可以是合并单元格范围内的任意一个单元格
      Parameters:
      cell - Cell
      Returns:
      合并单元格
    • getMergedCell

      public static MergedCell getMergedCell(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
      获取合并单元格 传入的x,y坐标(列行数)可以是合并单元格范围内的任意一个单元格
      Parameters:
      sheet - Sheet
      x - 列号,从0开始,可以是合并单元格范围中的任意一列
      y - 行号,从0开始,可以是合并单元格范围中的任意一行
      Returns:
      合并单元格,如果非合并单元格,返回坐标对应的单元格
    • setComment

      public static void setComment(org.apache.poi.ss.usermodel.Cell cell, String commentText, String commentAuthor)
      为特定单元格添加批注
      Parameters:
      cell - 单元格
      commentText - 批注内容
      commentAuthor - 作者
    • setComment

      public static void setComment(org.apache.poi.ss.usermodel.Cell cell, String commentText, String commentAuthor, org.apache.poi.ss.usermodel.ClientAnchor anchor)
      为特定单元格添加批注
      Parameters:
      cell - 单元格
      commentText - 批注内容
      commentAuthor - 作者,null表示无作者
      anchor - 批注的位置、大小等信息,null表示使用默认
    • remove

      public static void remove(org.apache.poi.ss.usermodel.Cell cell)
      移除指定单元格
      Parameters:
      cell - 单元格
    • of

      public static org.apache.poi.ss.util.CellRangeAddress of(String ref)
      根据字符串表示法创建一个CellRangeAddress对象 该方法提供了一种通过直接使用字符串引用来创建CellRangeAddress对象的便捷方式 字符串引用应符合Excel中单元格范围的常规表示法,如"B1" 或 "A1:B2"。
      Parameters:
      ref - 代表单元格范围的字符串,格式为"A1:B2"。
      Returns:
      根据给定字符串创建的CellRangeAddress对象。
      See Also:
      • CellRangeAddress.valueOf(String)
    • of

      public static org.apache.poi.ss.util.CellRangeAddress of(int firstRow, int lastRow, int firstCol, int lastCol)
      根据给定的行和列索引创建一个CellRangeAddress对象 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
      Parameters:
      firstRow - 起始行的索引,从0开始计数。
      lastRow - 结束行的索引,从0开始计数,必须大于firstRow。
      firstCol - 起始列的索引,从0开始计数。
      lastCol - 结束列的索引,从0开始计数,必须大于firstCol。
      Returns:
      返回一个新的CellRangeAddress对象,表示由参数定义的区域。
    • ofSingleRow

      public static org.apache.poi.ss.util.CellRangeAddress ofSingleRow(int row, int lastCol)
      根据给定的行和列索引创建一个单行的CellRangeAddress对象,从首列开始。 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
      Parameters:
      row - 行的索引,从0开始计数。
      lastCol - 结束列的索引,从0开始计数,必须大于firstCol。
      Returns:
      返回一个新的CellRangeAddress对象,表示由参数定义的区域。
    • ofSingleRow

      public static org.apache.poi.ss.util.CellRangeAddress ofSingleRow(int row, int firstCol, int lastCol)
      根据给定的行和列索引创建一个单行的CellRangeAddress对象 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
      Parameters:
      row - 行的索引,从0开始计数。
      firstCol - 起始列的索引,从0开始计数。
      lastCol - 结束列的索引,从0开始计数,必须大于firstCol。
      Returns:
      返回一个新的CellRangeAddress对象,表示由参数定义的区域。
    • ofSingleColumn

      public static org.apache.poi.ss.util.CellRangeAddress ofSingleColumn(int lastRow, int col)
      根据给定的行和列索引创建一个单列CellRangeAddress对象,从首行开始。 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
      Parameters:
      lastRow - 结束行的索引,从0开始计数,必须大于firstRow。
      col - 列的索引,从0开始计数。
      Returns:
      返回一个新的CellRangeAddress对象,表示由参数定义的区域。
    • ofSingleColumn

      public static org.apache.poi.ss.util.CellRangeAddress ofSingleColumn(int firstRow, int lastRow, int col)
      根据给定的行和列索引创建一个单列CellRangeAddress对象。 这个方法提供了一种简洁的方式,来表示一个Excel表格中特定的区域,由起始行和列以及结束行和列定义。
      Parameters:
      firstRow - 起始行的索引,从0开始计数。
      lastRow - 结束行的索引,从0开始计数,必须大于firstRow。
      col - 列的索引,从0开始计数。
      Returns:
      返回一个新的CellRangeAddress对象,表示由参数定义的区域。
    • getCellRangeAddress

      public static org.apache.poi.ss.util.CellRangeAddress getCellRangeAddress(org.apache.poi.ss.usermodel.Sheet sheet, String locationRef)
      获取合并单元格CellRangeAddress,如果不是返回null
      Parameters:
      sheet - Sheet
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      CellRangeAddress
    • getCellRangeAddress

      public static org.apache.poi.ss.util.CellRangeAddress getCellRangeAddress(org.apache.poi.ss.usermodel.Cell cell)
      获取合并单元格CellRangeAddress,如果不是返回null
      Parameters:
      cell - Cell
      Returns:
      CellRangeAddress
    • getCellRangeAddress

      public static org.apache.poi.ss.util.CellRangeAddress getCellRangeAddress(org.apache.poi.ss.usermodel.Sheet sheet, int x, int y)
      获取合并单元格CellRangeAddress,如果不是返回null
      Parameters:
      sheet - Sheet
      x - 列号,从0开始
      y - 行号,从0开始
      Returns:
      CellRangeAddress
    • indexToColName

      public static String indexToColName(int index)
      将Sheet列号变为列名
      Parameters:
      index - 列号, 从0开始
      Returns:
      0-A; 1-B...26-AA
    • colNameToIndex

      public static int colNameToIndex(String colName)
      根据表元的列名转换为列号
      Parameters:
      colName - 列名, 从A开始
      Returns:
      A1-0; B1-1...AA1-26
    • toCellReference

      public static org.apache.poi.ss.util.CellReference toCellReference(String locationRef)
      将Excel中地址标识符(例如A11,B5)等转换为行列表示 例如:A11 - col:0,row:10,B5-col:1,row:4
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      坐标点