T - 子类类型,用于返回thisC - ExcelConfig类型public class ExcelBase<T extends ExcelBase<T,C>,C extends ExcelConfig> extends Object implements Closeable
| Modifier and Type | Field and Description |
|---|---|
protected C |
config
Excel配置,此项不为空
|
protected boolean |
isClosed
是否被关闭
|
protected org.apache.poi.ss.usermodel.Sheet |
sheet
Excel中对应的Sheet
|
protected File |
targetFile
目标文件,如果用户读取为流或自行创建的Workbook或Sheet,此参数为
null |
protected org.apache.poi.ss.usermodel.Workbook |
workbook
工作簿
|
| Constructor and Description |
|---|
ExcelBase(C config,
org.apache.poi.ss.usermodel.Sheet sheet)
构造
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkClosed()
校验Excel是否已经关闭
|
T |
cloneSheet(int sheetIndex,
String newSheetName,
boolean setAsCurrentSheet)
复制当前sheet为新sheet
|
void |
close()
关闭工作簿
如果用户设定了目标文件,先写出目标文件后给关闭工作簿 |
org.apache.poi.ss.usermodel.CellStyle |
createCellStyle()
创建单元格样式
|
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 |
createColumnStyle(int x)
创建某一列的样式,返回样式后可以设置样式内容
|
org.apache.poi.ss.usermodel.Font |
createFont()
创建字体
|
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 |
createRowStyle(int y)
创建某一行的样式,返回样式后可以设置样式内容
|
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 |
getColumnCount()
获取第一行总列数,计算方法为:
最后一列序号 + 1
|
int |
getColumnCount(int rowNum)
获取总列数,计算方法为:
最后一列序号 + 1
|
C |
getConfig()
获取Excel配置
|
String |
getContentType()
获取Content-Type头对应的值,可以通过调用以下方法快速设置下载Excel的头信息:
response.setContentType(excelWriter.getContentType());
|
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 |
getOrCreateCellStyle(String locationRef)
为指定单元格获取或者创建样式,返回样式后可以设置样式内容
|
org.apache.poi.ss.usermodel.CellStyle |
getOrCreateColumnStyle(int x)
获取或创建某一列的样式,返回样式后可以设置样式内容
需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。 |
org.apache.poi.ss.usermodel.Row |
getOrCreateRow(int y)
获取或者创建行
|
org.apache.poi.ss.usermodel.CellStyle |
getOrCreateRowStyle(int y)
获取或创建某一行的样式,返回样式后可以设置样式内容
需要注意,此方法返回行样式,设置背景色在单元格设置值后会被覆盖,需要单独设置其单元格的样式。 |
int |
getPhysicalRowCount()
获取有记录的行数,计算方法为:
最后一行序号 - 第一行序号 + 1
|
int |
getRowCount()
获取总行数,计算方法为:
最后一行序号 + 1
|
org.apache.poi.ss.usermodel.Sheet |
getSheet()
获取当前Sheet
|
int |
getSheetCount()
返回工作簿表格数
|
List<String> |
getSheetNames()
获取表名列表
|
List<org.apache.poi.ss.usermodel.Sheet> |
getSheets()
获取此工作簿所有Sheet表
|
org.apache.poi.ss.usermodel.Workbook |
getWorkbook()
获取Workbook
|
boolean |
isXlsx()
判断是否为xlsx格式的Excel表(Excel07格式)
|
T |
renameSheet(String newName)
重命名当前sheet
|
T |
setConfig(C config)
设置Excel配置
|
T |
setSheet(int sheetIndex)
自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之(命名为默认)
在读取中,此方法用于切换读取的sheet,在写出时,此方法用于新建或者切换sheet |
T |
setSheet(org.apache.poi.ss.usermodel.Sheet sheet)
设置自定义Sheet
|
T |
setSheet(String sheetName)
自定义需要读取或写出的Sheet,如果给定的sheet不存在,创建之。
|
protected C extends ExcelConfig config
protected boolean isClosed
protected File targetFile
nullprotected org.apache.poi.ss.usermodel.Workbook workbook
protected org.apache.poi.ss.usermodel.Sheet sheet
public ExcelBase(C config, org.apache.poi.ss.usermodel.Sheet sheet)
config - configsheet - Excel中的sheetpublic C getConfig()
public org.apache.poi.ss.usermodel.Workbook getWorkbook()
public int getSheetCount()
public List<org.apache.poi.ss.usermodel.Sheet> getSheets()
public org.apache.poi.ss.usermodel.Sheet getSheet()
Sheetpublic T renameSheet(String newName)
newName - 新名字Workbook.setSheetName(int, String)public T setSheet(String sheetName)
sheetName - sheet名public T setSheet(int sheetIndex)
sheetIndex - sheet序号,从0开始计数public T setSheet(org.apache.poi.ss.usermodel.Sheet sheet)
sheet - 自定义sheet,可以通过SheetUtil.getOrCreateSheet(Workbook, String) 创建public T cloneSheet(int sheetIndex, String newSheetName, boolean setAsCurrentSheet)
sheetIndex - sheet位置newSheetName - 新sheet名setAsCurrentSheet - 是否切换为当前sheetpublic org.apache.poi.ss.usermodel.Cell getCell(String locationRef)
nulllocationRef - 单元格地址标识符,例如A11,B5Cellpublic org.apache.poi.ss.usermodel.Cell getCell(int x,
int y)
nullx - X坐标,从0计数,即列号y - Y坐标,从0计数,即行号Cellpublic org.apache.poi.ss.usermodel.Cell getOrCreateCell(String locationRef)
locationRef - 单元格地址标识符,例如A11,B5Cellpublic org.apache.poi.ss.usermodel.Cell getOrCreateCell(int x,
int y)
x - X坐标,从0计数,即列号y - Y坐标,从0计数,即行号Cellpublic org.apache.poi.ss.usermodel.Cell getCell(String locationRef, boolean isCreateIfNotExist)
nulllocationRef - 单元格地址标识符,例如A11,B5isCreateIfNotExist - 单元格不存在时是否创建Cellpublic org.apache.poi.ss.usermodel.Cell getCell(int x,
int y,
boolean isCreateIfNotExist)
nullx - X坐标,从0计数,即列号y - Y坐标,从0计数,即行号isCreateIfNotExist - 单元格不存在时是否创建Cellpublic org.apache.poi.ss.usermodel.Row getOrCreateRow(int y)
y - Y坐标,从0计数,即行号Rowpublic int getRowCount()
最后一行序号 + 1
public int getPhysicalRowCount()
最后一行序号 - 第一行序号 + 1
public org.apache.poi.ss.usermodel.CellStyle getOrCreateCellStyle(String locationRef)
locationRef - 单元格地址标识符,例如A11,B5CellStylepublic org.apache.poi.ss.usermodel.CellStyle getOrCreateCellStyle(int x,
int y)
x - X坐标,从0计数,即列号y - Y坐标,从0计数,即行号CellStylepublic org.apache.poi.ss.usermodel.CellStyle createCellStyle(String locationRef)
locationRef - 单元格地址标识符,例如A11,B5CellStylepublic org.apache.poi.ss.usermodel.CellStyle createCellStyle(int x,
int y)
x - X坐标,从0计数,即列号y - Y坐标,从0计数,即行号CellStylepublic org.apache.poi.ss.usermodel.CellStyle createCellStyle()
CellStyleWorkbook.createCellStyle()public org.apache.poi.ss.usermodel.CellStyle getOrCreateRowStyle(int y)
y - Y坐标,从0计数,即行号CellStylepublic org.apache.poi.ss.usermodel.CellStyle createRowStyle(int y)
y - Y坐标,从0计数,即行号CellStylepublic org.apache.poi.ss.usermodel.CellStyle getOrCreateColumnStyle(int x)
x - X坐标,从0计数,即列号CellStylepublic org.apache.poi.ss.usermodel.CellStyle createColumnStyle(int x)
x - X坐标,从0计数,即列号CellStylepublic org.apache.poi.ss.usermodel.Font createFont()
public org.apache.poi.ss.usermodel.Hyperlink createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type,
String address)
Hyperlink,默认内容(标签为链接地址本身)type - 链接类型address - 链接地址public org.apache.poi.ss.usermodel.Hyperlink createHyperlink(org.apache.poi.common.usermodel.HyperlinkType type,
String address,
String label)
Hyperlink,默认内容type - 链接类型address - 链接地址label - 标签,即单元格中显示的内容public int getColumnCount()
最后一列序号 + 1
public int getColumnCount(int rowNum)
最后一列序号 + 1
rowNum - 行号public boolean isXlsx()
public String getContentType()
response.setContentType(excelWriter.getContentType());
public void close()
close in interface Closeableclose in interface AutoCloseableprotected void checkClosed()
Copyright © 2025. All rights reserved.