Package org.miaixz.bus.office.excel
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void校验Excel是否已经关闭cloneSheet(int sheetIndex, String newSheetName, boolean setAsCurrentSheet) 复制当前sheet为新sheetvoidclose()关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿org.apache.poi.ss.usermodel.CellStyle创建单元格样式org.apache.poi.ss.usermodel.CellStylecreateCellStyle(int x, int y) 为指定单元格创建样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.CellStylecreateCellStyle(String locationRef) 为指定单元格创建样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.CellStylecreateColumnStyle(int x) 创建某一列的样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.Font创建字体org.apache.poi.ss.usermodel.HyperlinkcreateHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address) 创建Hyperlink,默认内容(标签为链接地址本身)org.apache.poi.ss.usermodel.HyperlinkcreateHyperlink(org.apache.poi.common.usermodel.HyperlinkType type, String address, String label) 创建Hyperlink,默认内容org.apache.poi.ss.usermodel.CellStylecreateRowStyle(int y) 创建某一行的样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.CellgetCell(int x, int y) 获取指定坐标单元格,单元格不存在时返回nullorg.apache.poi.ss.usermodel.CellgetCell(int x, int y, boolean isCreateIfNotExist) 获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回nullorg.apache.poi.ss.usermodel.Cell获取指定坐标单元格,单元格不存在时返回nullorg.apache.poi.ss.usermodel.Cell获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回nullint获取第一行总列数,计算方法为:intgetColumnCount(int rowNum) 获取总列数,计算方法为:获取Excel配置获取Content-Type头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:org.apache.poi.ss.usermodel.CellgetOrCreateCell(int x, int y) 获取或创建指定坐标单元格org.apache.poi.ss.usermodel.CellgetOrCreateCell(String locationRef) 获取或创建指定坐标单元格org.apache.poi.ss.usermodel.CellStylegetOrCreateCellStyle(int x, int y) 为指定单元格获取或者创建样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.CellStylegetOrCreateCellStyle(String locationRef) 为指定单元格获取或者创建样式,返回样式后可以设置样式内容org.apache.poi.ss.usermodel.CellStylegetOrCreateColumnStyle(int x) 获取或创建某一列的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。org.apache.poi.ss.usermodel.RowgetOrCreateRow(int y) 获取或者创建行org.apache.poi.ss.usermodel.CellStylegetOrCreateRowStyle(int y) 获取或创建某一行的样式,返回样式后可以设置样式内容 需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。int获取有记录的行数,计算方法为:int获取总行数,计算方法为:org.apache.poi.ss.usermodel.SheetgetSheet()获取当前Sheetint返回工作簿表格数获取表名列表List<org.apache.poi.ss.usermodel.Sheet> 获取此工作簿所有Sheet表org.apache.poi.ss.usermodel.Workbook获取WorkbookbooleanisXlsx()判断是否为xlsx格式的Excel表(Excel07格式)renameSheet(String newName) 重命名当前sheet设置Excel配置setSheet(int sheetIndex) 自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。setSheet(org.apache.poi.ss.usermodel.Sheet sheet) 设置自定义Sheet
-
Field Details
-
config
Excel配置,此项不为空 -
isClosed
protected boolean isClosed是否被关闭 -
targetFile
目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为null -
workbook
protected org.apache.poi.ss.usermodel.Workbook workbook工作簿 -
sheet
protected org.apache.poi.ss.usermodel.Sheet sheetExcel中对应的Sheet
-
-
Constructor Details
-
ExcelBase
构造- Parameters:
config- configsheet- Excel中的sheet
-
-
Method Details
-
getConfig
获取Excel配置- Returns:
- Excel配置
-
setConfig
设置Excel配置- Parameters:
config- Excel配置- Returns:
- this
-
getWorkbook
public org.apache.poi.ss.usermodel.Workbook getWorkbook()获取Workbook- Returns:
- Workbook
-
getSheetCount
public int getSheetCount()返回工作簿表格数- Returns:
- 工作簿表格数
-
getSheets
获取此工作簿所有Sheet表- Returns:
- sheet表列表
-
getSheetNames
获取表名列表- Returns:
- 表名列表
-
getSheet
public org.apache.poi.ss.usermodel.Sheet getSheet()获取当前Sheet- Returns:
Sheet
-
setSheet
自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet。- Parameters:
sheetName- sheet名- Returns:
- this
-
setSheet
自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet- Parameters:
sheetIndex- sheet序号,从0开始计数- Returns:
- this
-
setSheet
设置自定义Sheet- Parameters:
sheet- 自定义sheet,可以通过SheetKit.getOrCreateSheet(Workbook, String)创建- Returns:
- this
-
renameSheet
重命名当前sheet- Parameters:
newName- 新名字- Returns:
- this
- See Also:
-
cloneSheet
复制当前sheet为新sheet- Parameters:
sheetIndex- sheet位置newSheetName- 新sheet名setAsCurrentSheet- 是否切换为当前sheet- Returns:
- this
-
getCell
获取指定坐标单元格,单元格不存在时返回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
获取或创建指定坐标单元格- 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
获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null- Parameters:
locationRef- 单元格地址标识符,例如A11,B5isCreateIfNotExist- 单元格不存在时是否创建- 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
为指定单元格获取或者创建样式,返回样式后可以设置样式内容- 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
为指定单元格创建样式,返回样式后可以设置样式内容- 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:
-
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
获取Content-Type头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:response.setContentType(excelWriter.getContentType());
- Returns:
- Content-Type值
-
close
public void close()关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
checkClosed
protected void checkClosed()校验Excel是否已经关闭
-