注解+反射优雅的实现Excel导入导出(通用版)
大家好,我是D哥
点击关注下方公众号,Java面试资料 都在这里
来源:https://blog.csdn.net/youzi1394046585/article/details/86670203
日常在做后台系统的时候会很频繁的遇到Excel导入导出的问题,正好这次在做一个后台系统,就想着写一个公用工具来进行Excel的导入导出。
一般我们在导出的时候都是导出的前端表格,而前端表格同时也会对应的在后台有一个映射类。
所以在写这个工具的时候我们先理一下我们需要实现的效果:
导出方法接收一个list集合,和一个Class类型,和HttpServletResponse 对象
导出是可能会有下拉列表,所以需要一个map存储下拉列表数据源,传入参数后只需一行代码即可导出
导入方法需要传入file文件,以及一个Class类型,导入之后将会返回一个list集合,里面的对象就是传入类型的对象,传入参数后只需一行代码即可导入
# 实现过程
首先需要创建三个注解
一个是EnableExport ,必须有这个注解才能导出
/**
* 设置允许导出
*/
(ElementType.TYPE)
(RetentionPolicy.RUNTIME)
public EnableExport {
String fileName();
}
然后就是EnableExportField,有这个注解的字段才会导出到Excel里面,并且可以设置列宽
/**
* 设置该字段允许导出
* 并且可以设置宽度
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interfaceEnableExportField {
intcolWidth() default 100;
String colName();
}
再就是ImportIndex,导入的时候设置Excel中的列对应的序号
/**
* 导入时索引
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interfaceImportIndex {
intindex() ;
}
注解使用示例
# 三个注解创建好之后就需要开始操作Excel了
首先,导入方法。在后台接收到前端上传的Excel文件之后,使用poi来读取Excel文件
我们根据传入的类型上面的字段注解的顺序来分别为不同的字段赋值,然后存入集合中,再返回
代码如下:
/**
* 将Excel转换为对象集合
* @param excel Excel 文件
* @param clazz pojo类型
* @return
*/
publicstatic List<Object> parseExcelToList(File excel,Class clazz){
List<Object> res = new ArrayList<>();
// 创建输入流,读取Excel
InputStream is = null;
Sheet sheet = null;
try {
is = new FileInputStream(excel.getAbsolutePath());
if (is != null) {
Workbook workbook = WorkbookFactory.create(is);
//默认只获取第一个工作表
sheet = workbook.getSheetAt(0);
if (sheet != null) {
//前两行是标题
int i = 2;
String values[] ;
Row row = sheet.getRow(i);
while (row != null) {
//获取单元格数目
int cellNum = row.getPhysicalNumberOfCells();
values = new String[cellNum];
for (int j = 0; j <= cellNum; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
//设置单元格内容类型
cell.setCellType(Cell.CELL_TYPE_STRING );
//获取单元格值
String value = cell.getStringCellValue() == null ? null : cell.getStringCellValue();
values[j]=value;
}
}
Field[] fields = clazz.getDeclaredFields();
Object obj = clazz.newInstance();
for(Field f : fields){
if(f.isAnnotationPresent(ImportIndex.class)){
ImportIndex annotation = f.getDeclaredAnnotation(ImportIndex.class);
int index = annotation.index();
f.setAccessible(true);
//此处使用了阿里巴巴的fastjson包里面的一个类型转换工具类
Object val =TypeUtils.cast(values[index],f.getType(),null);
f.set(obj,val);
}
}
res.add(obj);
i++;
row=sheet.getRow(i);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
接下来就是导出方法
导出分为几个步骤:
- 建立一个工作簿,也就是类型新建一个Excel文件
- 建立一张sheet表
- 设置标的行高和列宽
- 绘制标题和表头
这两个方法是自定义方法,代码会贴在后面
- 写入数据到Excel
- 创建下拉列表
- 写入文件到response
到这里导出工作就完成了
# 下面是一些自定义方法的代码
/**
* 获取一个基本的带边框的单元格
* @param workbook
* @return
*/
privatestatic HSSFCellStyle getBasicCellStyle(HSSFWorkbook workbook){
HSSFCellStyle hssfcellstyle = workbook.createCellStyle();
hssfcellstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
hssfcellstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
hssfcellstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
hssfcellstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
hssfcellstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
hssfcellstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
hssfcellstyle.setWrapText(true);
return hssfcellstyle;
}
/**
* 获取带有背景色的标题单元格
* @param workbook
* @return
*/
privatestatic HSSFCellStyle getTitleCellStyle(HSSFWorkbook workbook){
HSSFCellStyle hssfcellstyle = getBasicCellStyle(workbook);
hssfcellstyle.setFillForegroundColor((short) HSSFColor.CORNFLOWER_BLUE.index); // 设置背景色
hssfcellstyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
return hssfcellstyle;
}
/**
* 创建一个跨列的标题行
* @param workbook
* @param hssfRow
* @param hssfcell
* @param hssfsheet
* @param allColNum
* @param title
*/
privatestaticvoidcreateTitle(HSSFWorkbook workbook, HSSFRow hssfRow , HSSFCell hssfcell, HSSFSheet hssfsheet,int allColNum,String title){
//在sheet里增加合并单元格
CellRangeAddress cra = new CellRangeAddress(0, 0, 0, allColNum);
hssfsheet.addMergedRegion(cra);
// 使用RegionUtil类为合并后的单元格添加边框
RegionUtil.setBorderBottom(1, cra, hssfsheet, workbook); // 下边框
RegionUtil.setBorderLeft(1, cra, hssfsheet, workbook); // 左边框
RegionUtil.setBorderRight(1, cra, hssfsheet, workbook); // 有边框
RegionUtil.setBorderTop(1, cra, hssfsheet, workbook); // 上边框
//设置表头
hssfRow = hssfsheet.getRow(0);
hssfcell = hssfRow.getCell(0);
hssfcell.setCellStyle( getTitleCellStyle(workbook));
hssfcell.setCellType(HSSFCell.CELL_TYPE_STRING);
hssfcell.setCellValue(title);
}
/**
* 设置表头标题栏以及表格高度
* @param workbook
* @param hssfRow
* @param hssfcell
* @param hssfsheet
* @param colNames
*/
privatestaticvoidcreateHeadRow(HSSFWorkbook workbook,HSSFRow hssfRow , HSSFCell hssfcell,HSSFSheet hssfsheet,List<String> colNames){
//插入标题行
hssfRow = hssfsheet.createRow(1);
for (int i = 0; i < colNames.size(); i++) {
hssfcell = hssfRow.createCell(i);
hssfcell.setCellStyle(getTitleCellStyle(workbook));
hssfcell.setCellType(HSSFCell.CELL_TYPE_STRING);
hssfcell.setCellValue(colNames.get(i));
}
}
/**
* excel添加下拉数据校验
* @param sheet 哪个 sheet 页添加校验
* @return
*/
publicstaticvoidcreateDataValidation(Sheet sheet,Map<Integer,String[]> selectListMap){
if(selectListMap!=null) {
selectListMap.forEach(
// 第几列校验(0开始)key 数据源数组value
(key, value) -> {
if(value.length>0) {
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, key, key);
DataValidationHelper helper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = helper.createExplicitListConstraint(value);
DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
//处理Excel兼容性问题
if (dataValidation instanceof XSSFDataValidation) {
dataValidation.setSuppressDropDownArrow(true);
dataValidation.setShowErrorBox(true);
} else {
dataValidation.setSuppressDropDownArrow(false);
}
dataValidation.setEmptyCellAllowed(true);
dataValidation.setShowPromptBox(true);
dataValidation.createPromptBox("提示", "只能选择下拉框里面的数据");
sheet.addValidationData(dataValidation);
}
}
);
}
}
使用实例
导出数据
导入数据(返回对象List)
源码地址:https://github.com/xyz0101/excelutils
最后,D哥也建了一个技术群,主要探讨一些新的技术和开源项目值不值得去研究及IDEA使用的“骚操作”,有兴趣入群的同学,可长按扫描下方二维码,一定要备注:城市+昵称+技术方向,根据格式备注,可更快被通过且邀请进群。
▲长按扫描
关键词
方法
对象
Excel文件
代码
注解
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。