public class ExcelUtils<T> extends Object
| Modifier and Type | Field and Description |
|---|---|
Class<T> |
clazz
实体对象
|
static String |
FORMULA_REGEX_STR |
static String[] |
FORMULA_STR |
static int |
sheetSize
Excel sheet最大行数,默认65536
|
| Constructor and Description |
|---|
ExcelUtils(Class<T> clazz)
构造方法,初始化工具类
|
| Modifier and Type | Method and Description |
|---|---|
org.apache.poi.ss.usermodel.Cell |
addCell(Excel attr,
org.apache.poi.ss.usermodel.Row row,
T vo,
Field field,
int column)
添加单元格数据到指定行和列。
|
void |
addStatisticsRow()
创建统计行并添加到当前工作表的末尾。
|
static String |
convertByExp(String propertyValue,
String converterExp,
String separator)
根据表达式将属性值转换为对应的显示值。
|
org.apache.poi.ss.usermodel.Cell |
createCell(Excel attr,
org.apache.poi.ss.usermodel.Row row,
int column)
创建单元格并设置表头样式。
|
void |
createSheet(int sheetNo,
int index)
创建工作表
在需要多个 sheet 时(sheetNo 大于 1),并且当前是第 2 个及以后的 sheet(index 大于 0)
为工作簿创建一个新工作表,设置表头标题,并将该 sheet 命名为 sheetName+index
这样可以实现多 sheet 导出时,每个 sheet 都有独立的标题和名称。
|
void |
createTitle()
创建 Excel 的标题行(第一行)。
|
void |
createWorkbook()
创建一个工作簿
|
String |
dataFormatHandlerAdapter(Object value,
Excel excel)
使用自定义数据处理器格式化单元格数据。
|
void |
exportExcel(jakarta.servlet.http.HttpServletResponse response)
将当前工作簿内容写入 HTTP 响应流,实现 Excel 文件导出。
|
void |
exportExcel(jakarta.servlet.http.HttpServletResponse response,
List<T> list,
String sheetName)
导出数据到 Excel 文件。
|
void |
exportExcel(jakarta.servlet.http.HttpServletResponse response,
List<T> list,
String sheetName,
String title)
导出数据到 Excel 文件,并设置标题。
|
void |
fillExcelData(int index,
org.apache.poi.ss.usermodel.Row row)
填充 Excel 数据到当前 Sheet。
|
Object |
getCellValue(org.apache.poi.ss.usermodel.Row row,
int column)
获取指定行和列的单元格值,并根据单元格类型进行转换。
|
static org.apache.poi.ss.usermodel.Drawing<?> |
getDrawingPatriarch(org.apache.poi.ss.usermodel.Sheet sheet)
获取或创建指定 Sheet 的画布对象(Drawing)。
|
List<Object[]> |
getFields()
获取类中所有带有 @Excel 或 @Excels 注解的字段信息。
|
int |
getImageType(byte[] value)
根据图片字节内容判断图片类型,返回 POI 对应的图片类型常量。
|
short |
getRowHeight()
根据注解获取最大行高
|
List<T> |
importExcel(InputStream is)
将 Excel 数据导入为对象列表。
|
List<T> |
importExcel(InputStream is,
int titleNum)
将 Excel 数据导入为对象列表。
|
List<T> |
importExcel(String sheetName,
InputStream is,
int titleNum)
将 Excel 数据导入为对象列表。
|
void |
importTemplateExcel(jakarta.servlet.http.HttpServletResponse response,
String sheetName)
对list数据源将其里面的数据导入到excel表单
|
void |
importTemplateExcel(jakarta.servlet.http.HttpServletResponse response,
String sheetName,
String title)
对list数据源将其里面的数据导入到excel表单
|
void |
init(List<T> list,
String sheetName,
String title,
TypeEnum type)
初始化 Excel 工具类,设置数据源、工作表名称、标题和导出类型。
|
String |
parseDateToStr(String dateFormat,
Object val)
根据指定格式将不同类型的日期对象格式化为字符串。
|
static String |
reverseByExp(String propertyValue,
String converterExp,
String separator)
根据表达式将显示值反向转换为属性值。
|
void |
setCellVo(Object value,
Excel attr,
org.apache.poi.ss.usermodel.Cell cell)
设置单元格的值和样式。
|
void |
setDataCell(org.apache.poi.ss.usermodel.Cell cell,
Excel excel)
设置单元格的样式。
|
void |
setDataValidation(Excel attr,
org.apache.poi.ss.usermodel.Row row,
int column)
设置单元格的数据校验和列宽。
|
void |
setPromptOrValidation(org.apache.poi.ss.usermodel.Sheet sheet,
String[] textlist,
String promptContent,
int firstRow,
int endRow,
int firstCol,
int endCol)
为指定的 Excel 区域设置单元格下拉框或提示框数据校验。
|
void |
writeSheet()
写入数据到 Excel 工作表。
|
public static final String FORMULA_REGEX_STR
public static final String[] FORMULA_STR
public static final int sheetSize
public void init(List<T> list, String sheetName, String title, TypeEnum type)
list - 数据源sheetName - 工作表名称title - 标题type - 导出类型(EXPORT 或 IMPORT)public void createTitle()
public List<T> importExcel(InputStream is) throws Exception
is - 输入流Exception - 如果解析失败public List<T> importExcel(InputStream is, int titleNum) throws Exception
is - 输入流titleNum - 标题占用行数Exception - 如果解析失败public List<T> importExcel(String sheetName, InputStream is, int titleNum) throws Exception
sheetName - 工作表名称is - 输入流titleNum - 标题占用行数Exception - 如果解析失败public void exportExcel(jakarta.servlet.http.HttpServletResponse response,
List<T> list,
String sheetName)
response - HTTP 响应对象list - 数据源sheetName - 工作表名称public void exportExcel(jakarta.servlet.http.HttpServletResponse response,
List<T> list,
String sheetName,
String title)
response - HTTP 响应对象list - 数据源sheetName - 工作表名称title - 标题public void importTemplateExcel(jakarta.servlet.http.HttpServletResponse response,
String sheetName)
response - HTTP 响应对象sheetName - 工作表名称public void importTemplateExcel(jakarta.servlet.http.HttpServletResponse response,
String sheetName,
String title)
response - HTTP 响应对象sheetName - 工作表名称title - 标题public void exportExcel(jakarta.servlet.http.HttpServletResponse response)
response - HTTP 响应对象public void writeSheet()
public void fillExcelData(int index,
org.apache.poi.ss.usermodel.Row row)
index - 当前 Sheet 的索引row - 当前表头行对象public org.apache.poi.ss.usermodel.Cell createCell(Excel attr, org.apache.poi.ss.usermodel.Row row, int column)
attr - Excel 注解row - 行对象column - 列索引public void setCellVo(Object value, Excel attr, org.apache.poi.ss.usermodel.Cell cell)
value - 单元格值attr - Excel注解cell - 单元格对象public static org.apache.poi.ss.usermodel.Drawing<?> getDrawingPatriarch(org.apache.poi.ss.usermodel.Sheet sheet)
sheet - 工作表对象public int getImageType(byte[] value)
Workbook.PICTURE_TYPE_JPEGWorkbook.PICTURE_TYPE_PNGvalue - 图片字节数组public void setDataValidation(Excel attr, org.apache.poi.ss.usermodel.Row row, int column)
attr - Excel注解row - 行对象column - 列索引public org.apache.poi.ss.usermodel.Cell addCell(Excel attr, org.apache.poi.ss.usermodel.Row row, T vo, Field field, int column)
attr - Excel 注解row - 行对象vo - 数据对象field - 字段对象column - 列索引public void setDataCell(org.apache.poi.ss.usermodel.Cell cell,
Excel excel)
cell - 单元格对象excel - Excel 注解信息public void setPromptOrValidation(org.apache.poi.ss.usermodel.Sheet sheet,
String[] textlist,
String promptContent,
int firstRow,
int endRow,
int firstCol,
int endCol)
sheet - 工作表对象textlist - 下拉框选项内容数组promptContent - 提示内容firstRow - 起始行endRow - 结束行firstCol - 起始列endCol - 结束列public static String convertByExp(String propertyValue, String converterExp, String separator)
propertyValue - 属性值converterExp - 转换表达式(如 0=男,1=女)separator - 多值分隔符public static String reverseByExp(String propertyValue, String converterExp, String separator)
propertyValue - 显示值converterExp - 转换表达式(如 0=男,1=女)separator - 多值分隔符public String dataFormatHandlerAdapter(Object value, Excel excel)
value - 原始数据值excel - Excel 注解信息(包含 handler 和参数)public void addStatisticsRow()
public List<Object[]> getFields()
public short getRowHeight()
public void createWorkbook()
public void createSheet(int sheetNo,
int index)
sheetNo - sheet数量index - 序号public Object getCellValue(org.apache.poi.ss.usermodel.Row row, int column)
row - 行对象column - 列号public String parseDateToStr(String dateFormat, Object val)
dateFormat - 日期格式字符串val - 需要格式化的日期对象Copyright © 2025. All rights reserved.