Package org.aoju.bus.office.excel
Class ExcelBase<T extends ExcelBase<T>>
java.lang.Object
org.aoju.bus.office.excel.ExcelBase<T>
- Type Parameters:
T- 子类类型,用于返回this
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
ExcelReader,ExcelWriter
Excel基础类,用于抽象ExcelWriter和ExcelReader中共用部分的对象和方法
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected File目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为null标题行别名protected boolean是否被关闭protected org.apache.poi.ss.usermodel.SheetExcel中对应的Sheetprotected org.apache.poi.ss.usermodel.Workbook工作簿 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddHeaderAlias(String header, String alias) 增加标题别名清空标题别名,key为Map中的key,value为别名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.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.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) 获取总列数,计算方法为:获得标题行的别名Maporg.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获取WorkbookremoveHeaderAlias(String header) 去除标题别名renameSheet(String newName) 重命名当前sheetsetHeaderAlias(Map<String, String> headerAlias) 设置标题行的别名MapsetSheet(int sheetIndex) 自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认) 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之 在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheetsetSheet(org.apache.poi.ss.usermodel.Sheet sheet) 设置自定义Sheet
-
Field Details
-
isClosed
protected boolean isClosed是否被关闭 -
destFile
目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为null -
workbook
protected org.apache.poi.ss.usermodel.Workbook workbook工作簿 -
sheet
protected org.apache.poi.ss.usermodel.Sheet sheetExcel中对应的Sheet -
headerAlias
标题行别名
-
-
Constructor Details
-
ExcelBase
public ExcelBase(org.apache.poi.ss.usermodel.Sheet sheet) 构造- Parameters:
sheet- Excel中的sheet
-
-
Method Details
-
getWorkbook
public org.apache.poi.ss.usermodel.Workbook getWorkbook()获取Workbook- Returns:
- Workbook
-
getSheetCount
public int getSheetCount()返回工作簿表格数- Returns:
- 工作簿表格数
-
getSheets
获取此工作簿所有Sheet表- Returns:
- sheet表列表
-
getSheetNames
获取表名列表- Returns:
- 表名列表
-
renameSheet
重命名当前sheet- Parameters:
newName- 新名字- Returns:
- this
- See Also:
-
Workbook.setSheetName(int, String)
-
cloneSheet
复制当前sheet为新sheet- Parameters:
sheetIndex- sheet位置newSheetName- 新sheet名setAsCurrentSheet- 是否切换为当前sheet- Returns:
- this
-
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,可以通过WorksKit.getOrCreateSheet(Workbook, String)创建- Returns:
- this
-
getCell
public org.apache.poi.ss.usermodel.Cell getCell(int x, int y) 获取指定坐标单元格,单元格不存在时返回null- Parameters:
x- X坐标,从0计数,既列号y- Y坐标,从0计数,既行号- Returns:
Cell
-
getCell
获取指定坐标单元格,单元格不存在时返回null- 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
-
getOrCreateCell
获取或创建指定坐标单元格- Parameters:
locationRef- 单元格地址标识符,例如A11,B5- 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
-
getCell
获取指定坐标单元格,如果isCreateIfNotExist为false,则在单元格不存在时返回null- Parameters:
locationRef- 单元格地址标识符,例如A11,B5isCreateIfNotExist- 单元格不存在时是否创建- Returns:
Cell
-
getOrCreateRow
public org.apache.poi.ss.usermodel.Row getOrCreateRow(int y) 获取或者创建行- Parameters:
y- Y坐标,从0计数,既行号- Returns:
Row
-
getOrCreateCellStyle
public org.apache.poi.ss.usermodel.CellStyle getOrCreateCellStyle(int x, int y) 为指定单元格获取或者创建样式,返回样式后可以设置样式内容- Parameters:
x- X坐标,从0计数,既列号y- Y坐标,从0计数,既行号- Returns:
CellStyle
-
getOrCreateRowStyle
public org.apache.poi.ss.usermodel.CellStyle getOrCreateRowStyle(int y) 获取或创建某一行的样式,返回样式后可以设置样式内容- Parameters:
y- Y坐标,从0计数,既行号- Returns:
CellStyle
-
createColumnStyle
public org.apache.poi.ss.usermodel.CellStyle createColumnStyle(int x) 创建某一列的样式,返回样式后可以设置样式内容- Parameters:
x- X坐标,从0计数,即列号- Returns:
CellStyle
-
getOrCreateColumnStyle
public org.apache.poi.ss.usermodel.CellStyle getOrCreateColumnStyle(int x) 获取或创建某一列的样式,返回样式后可以设置样式内容- Parameters:
x- X坐标,从0计数,既列号- Returns:
CellStyle
-
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:
- 链接
-
createCellStyle
public org.apache.poi.ss.usermodel.CellStyle createCellStyle(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()创建单元格样式- Returns:
CellStyle- See Also:
-
Workbook.createCellStyle()
-
getOrCreateCellStyle
为指定单元格获取或者创建样式,返回样式后可以设置样式内容- Parameters:
locationRef- 单元格地址标识符,例如A11,B5- Returns:
CellStyle
-
getRowCount
public int getRowCount()获取总行数,计算方法为:最后一行序号 + 1
- Returns:
- 行数
-
getPhysicalRowCount
public int getPhysicalRowCount()获取有记录的行数,计算方法为:最后一行序号 - 第一行序号 + 1
- Returns:
- 行数
-
getColumnCount
public int getColumnCount()获取第一行总列数,计算方法为:最后一列序号 + 1
- Returns:
- 列数
-
getColumnCount
public int getColumnCount(int rowNum) 获取总列数,计算方法为:最后一列序号 + 1
- Parameters:
rowNum- 行号- Returns:
- 列数
-
getHeaderAlias
获得标题行的别名Map- Returns:
- 别名Map
-
setHeaderAlias
设置标题行的别名Map- Parameters:
headerAlias- 别名Map- Returns:
- this
-
addHeaderAlias
增加标题别名- Parameters:
header- 标题alias- 别名- Returns:
- this
-
removeHeaderAlias
去除标题别名- Parameters:
header- 标题- Returns:
- this
-
clearHeaderAlias
清空标题别名,key为Map中的key,value为别名- Returns:
- this
-
close
public void close()关闭工作簿 如果用户设定了目标文件,先写出目标文件后给关闭工作簿- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-