Class ExcelBase<T extends ExcelBase<T,C>,C extends ExcelConfig>

java.lang.Object
org.miaixz.bus.office.excel.ExcelBase<T,C>
Type Parameters:
T - 子类类型,用于返回this
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
ExcelReader, ExcelWriter

public class ExcelBase<T extends ExcelBase<T,C>,C extends ExcelConfig> extends Object implements Closeable
Excel基础类,用于抽象ExcelWriter和ExcelReader中共用部分的对象和方法
Since:
Java 17+
Author:
Kimi Liu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected C
    Excel配置,此项不为空
    protected boolean
    是否被关闭
    protected org.apache.poi.ss.usermodel.Sheet
    Excel中对应的Sheet
    protected File
    目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为null
    protected org.apache.poi.ss.usermodel.Workbook
    工作簿
  • Constructor Summary

    Constructors
    Constructor
    Description
    ExcelBase(C config, org.apache.poi.ss.usermodel.Sheet sheet)
    构造
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    校验Excel是否已经关闭
    cloneSheet(int sheetIndex, String newSheetName, boolean setAsCurrentSheet)
    复制当前sheet为新sheet
    void
    关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿
    org.apache.poi.ss.usermodel.CellStyle
    创建单元格样式
    org.apache.poi.ss.usermodel.CellStyle
    createCellStyle(int x, int y)
    为指定单元格创建样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.CellStyle
    createCellStyle(String locationRef)
    为指定单元格创建样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.CellStyle
    创建某一列的样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.Font
    创建字体
    org.apache.poi.ss.usermodel.Hyperlink
    createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address)
    创建 Hyperlink,默认内容(标签为链接地址本身)
    org.apache.poi.ss.usermodel.Hyperlink
    createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address, String label)
    创建 Hyperlink,默认内容
    org.apache.poi.ss.usermodel.CellStyle
    创建某一行的样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.Cell
    getCell(int x, int y)
    获取指定坐标单元格,单元格不存在时返回null
    org.apache.poi.ss.usermodel.Cell
    getCell(int x, int y, boolean isCreateIfNotExist)
    获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null
    org.apache.poi.ss.usermodel.Cell
    getCell(String locationRef)
    获取指定坐标单元格,单元格不存在时返回null
    org.apache.poi.ss.usermodel.Cell
    getCell(String locationRef, boolean isCreateIfNotExist)
    获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null
    int
    获取第一行总列数,计算方法为:
    int
    getColumnCount(int rowNum)
    获取总列数,计算方法为:
    获取Excel配置
    获取Content-Type头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:
    org.apache.poi.ss.usermodel.Cell
    getOrCreateCell(int x, int y)
    获取或创建指定坐标单元格
    org.apache.poi.ss.usermodel.Cell
    getOrCreateCell(String locationRef)
    获取或创建指定坐标单元格
    org.apache.poi.ss.usermodel.CellStyle
    getOrCreateCellStyle(int x, int y)
    为指定单元格获取或者创建样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.CellStyle
    为指定单元格获取或者创建样式,返回样式后可以设置样式内容
    org.apache.poi.ss.usermodel.CellStyle
    获取或创建某一列的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。
    org.apache.poi.ss.usermodel.Row
    获取或者创建行
    org.apache.poi.ss.usermodel.CellStyle
    获取或创建某一行的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。
    int
    获取有记录的行数,计算方法为:
    int
    获取总行数,计算方法为:
    org.apache.poi.ss.usermodel.Sheet
    获取当前Sheet
    int
    返回工作簿表格数
    获取表名列表
    List<org.apache.poi.ss.usermodel.Sheet>
    获取此工作簿所有Sheet表
    org.apache.poi.ss.usermodel.Workbook
    获取Workbook
    boolean
    判断是否为xlsx格式的Excel表(Excel07格式)
    重命名当前sheet
    setConfig(C config)
    设置Excel配置
    setSheet(int sheetIndex)
    自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet
    setSheet(String sheetName)
    自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。
    setSheet(org.apache.poi.ss.usermodel.Sheet sheet)
    设置自定义Sheet

    Methods inherited from class java.lang.Object

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

    • config

      protected C extends ExcelConfig config
      Excel配置,此项不为空
    • isClosed

      protected boolean isClosed
      是否被关闭
    • targetFile

      protected File targetFile
      目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为null
    • workbook

      protected org.apache.poi.ss.usermodel.Workbook workbook
      工作簿
    • sheet

      protected org.apache.poi.ss.usermodel.Sheet sheet
      Excel中对应的Sheet
  • Constructor Details

    • ExcelBase

      public ExcelBase(C config, org.apache.poi.ss.usermodel.Sheet sheet)
      构造
      Parameters:
      config - config
      sheet - Excel中的sheet
  • Method Details

    • getConfig

      public C getConfig()
      获取Excel配置
      Returns:
      Excel配置
    • setConfig

      public T setConfig(C config)
      设置Excel配置
      Parameters:
      config - Excel配置
      Returns:
      this
    • getWorkbook

      public org.apache.poi.ss.usermodel.Workbook getWorkbook()
      获取Workbook
      Returns:
      Workbook
    • getSheetCount

      public int getSheetCount()
      返回工作簿表格数
      Returns:
      工作簿表格数
    • getSheets

      public List<org.apache.poi.ss.usermodel.Sheet> getSheets()
      获取此工作簿所有Sheet表
      Returns:
      sheet表列表
    • getSheetNames

      public List<String> getSheetNames()
      获取表名列表
      Returns:
      表名列表
    • getSheet

      public org.apache.poi.ss.usermodel.Sheet getSheet()
      获取当前Sheet
      Returns:
      Sheet
    • setSheet

      public T setSheet(String sheetName)
      自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet。
      Parameters:
      sheetName - sheet名
      Returns:
      this
    • setSheet

      public T setSheet(int sheetIndex)
      自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet
      Parameters:
      sheetIndex - sheet序号,从0开始计数
      Returns:
      this
    • setSheet

      public T setSheet(org.apache.poi.ss.usermodel.Sheet sheet)
      设置自定义Sheet
      Parameters:
      sheet - 自定义sheet,可以通过SheetKit.getOrCreateSheet(Workbook, String) 创建
      Returns:
      this
    • renameSheet

      public T renameSheet(String newName)
      重命名当前sheet
      Parameters:
      newName - 新名字
      Returns:
      this
      See Also:
      • Workbook.setSheetName(int, String)
    • cloneSheet

      public T cloneSheet(int sheetIndex, String newSheetName, boolean setAsCurrentSheet)
      复制当前sheet为新sheet
      Parameters:
      sheetIndex - sheet位置
      newSheetName - 新sheet名
      setAsCurrentSheet - 是否切换为当前sheet
      Returns:
      this
    • getCell

      public org.apache.poi.ss.usermodel.Cell getCell(String locationRef)
      获取指定坐标单元格,单元格不存在时返回null
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      Cell
    • getCell

      public org.apache.poi.ss.usermodel.Cell getCell(int x, int y)
      获取指定坐标单元格,单元格不存在时返回null
      Parameters:
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      Returns:
      Cell
    • getOrCreateCell

      public org.apache.poi.ss.usermodel.Cell getOrCreateCell(String locationRef)
      获取或创建指定坐标单元格
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      Cell
    • getOrCreateCell

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

      public org.apache.poi.ss.usermodel.Cell getCell(String locationRef, boolean isCreateIfNotExist)
      获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      isCreateIfNotExist - 单元格不存在时是否创建
      Returns:
      Cell
    • getCell

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

      public org.apache.poi.ss.usermodel.Row getOrCreateRow(int y)
      获取或者创建行
      Parameters:
      y - Y坐标,从0计数,即行号
      Returns:
      Row
    • getRowCount

      public int getRowCount()
      获取总行数,计算方法为:
       最后一行序号 + 1
       
      Returns:
      行数
    • getPhysicalRowCount

      public int getPhysicalRowCount()
      获取有记录的行数,计算方法为:
       最后一行序号 - 第一行序号 + 1
       
      Returns:
      行数
    • getOrCreateCellStyle

      public org.apache.poi.ss.usermodel.CellStyle getOrCreateCellStyle(String locationRef)
      为指定单元格获取或者创建样式,返回样式后可以设置样式内容
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      CellStyle
    • getOrCreateCellStyle

      public org.apache.poi.ss.usermodel.CellStyle getOrCreateCellStyle(int x, int y)
      为指定单元格获取或者创建样式,返回样式后可以设置样式内容
      Parameters:
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      Returns:
      CellStyle
    • createCellStyle

      public org.apache.poi.ss.usermodel.CellStyle createCellStyle(String locationRef)
      为指定单元格创建样式,返回样式后可以设置样式内容
      Parameters:
      locationRef - 单元格地址标识符,例如A11,B5
      Returns:
      CellStyle
    • createCellStyle

      public org.apache.poi.ss.usermodel.CellStyle createCellStyle(int x, int y)
      为指定单元格创建样式,返回样式后可以设置样式内容
      Parameters:
      x - X坐标,从0计数,即列号
      y - Y坐标,从0计数,即行号
      Returns:
      CellStyle
    • createCellStyle

      public org.apache.poi.ss.usermodel.CellStyle createCellStyle()
      创建单元格样式
      Returns:
      CellStyle
      See Also:
      • Workbook.createCellStyle()
    • getOrCreateRowStyle

      public org.apache.poi.ss.usermodel.CellStyle getOrCreateRowStyle(int y)
      获取或创建某一行的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。
      Parameters:
      y - Y坐标,从0计数,即行号
      Returns:
      CellStyle
    • createRowStyle

      public org.apache.poi.ss.usermodel.CellStyle createRowStyle(int y)
      创建某一行的样式,返回样式后可以设置样式内容
      Parameters:
      y - Y坐标,从0计数,即行号
      Returns:
      CellStyle
    • getOrCreateColumnStyle

      public org.apache.poi.ss.usermodel.CellStyle getOrCreateColumnStyle(int x)
      获取或创建某一列的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。
      Parameters:
      x - X坐标,从0计数,即列号
      Returns:
      CellStyle
    • createColumnStyle

      public org.apache.poi.ss.usermodel.CellStyle createColumnStyle(int x)
      创建某一列的样式,返回样式后可以设置样式内容
      Parameters:
      x - X坐标,从0计数,即列号
      Returns:
      CellStyle
    • createFont

      public org.apache.poi.ss.usermodel.Font createFont()
      创建字体
      Returns:
      字体
    • createHyperlink

      public org.apache.poi.ss.usermodel.Hyperlink createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address)
      创建 Hyperlink,默认内容(标签为链接地址本身)
      Parameters:
      type - 链接类型
      address - 链接地址
      Returns:
      链接
    • createHyperlink

      public org.apache.poi.ss.usermodel.Hyperlink createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address, String label)
      创建 Hyperlink,默认内容
      Parameters:
      type - 链接类型
      address - 链接地址
      label - 标签,即单元格中显示的内容
      Returns:
      链接
    • getColumnCount

      public int getColumnCount()
      获取第一行总列数,计算方法为:
       最后一列序号 + 1
       
      Returns:
      列数
    • getColumnCount

      public int getColumnCount(int rowNum)
      获取总列数,计算方法为:
       最后一列序号 + 1
       
      Parameters:
      rowNum - 行号
      Returns:
      列数,-1表示获取失败
    • isXlsx

      public boolean isXlsx()
      判断是否为xlsx格式的Excel表(Excel07格式)
      Returns:
      是否为xlsx格式的Excel表(Excel07格式)
    • getContentType

      public String getContentType()
      获取Content-Type头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:
       response.setContentType(excelWriter.getContentType());
       
      Returns:
      Content-Type值
    • close

      public void close()
      关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • checkClosed

      protected void checkClosed()
      校验Excel是否已经关闭