org.apache.poi
poi
4.1.2
org.apache.poi
poi-ooxml
4.1.2
org.apache.poi
poi-ooxml-schemas
4.1.2
我们结合自定义注解,让导出使用起来更方便简洁且更容易扩展。
@ExcelField
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelField {
/**
* 字段名
*/
String name() default "";
/**
* 字段顺序
*/
int order() default 1;
/**
* 单元格宽度
*/
int width() default 100;
/**
* 日期格式
*/
String dateFormat() default "yyyy-MM-dd HH:mm:ss";
}
@ExcelHeader
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelHeader {
/**
* 文件名称
* @return
*/
String fileName() default "导出文件";
/**
* sheet页名称
* @return
*/
String sheetName() default "sheet1";
/**
* 表头是否加粗
* @return
*/
boolean headBold() default false;
/**
* 表头颜色
* @return
*/
IndexedColors headColor() default IndexedColors.GREEN;
}
public class ExcelExportUtil {
/*
* @description: 导出
* @create: 2023/9/12 16:28
* @param response
* @param data
* @param clazz
* @return void
*/
public static void export(HttpServletResponse response, List data, Class clazz){
if(ObjectUtils.isEmpty(response) || ObjectUtils.isEmpty(data)){
return;
}
//解析表头
ExcelHeader excelHeader = getExcelHeader(clazz);
if(ObjectUtils.isEmpty(excelHeader)){
return;
}
//解析字段
List keyList = new ArrayList<>();
List excelFieldList = new ArrayList<>();
getExcelFields(clazz, keyList, excelFieldList);
if(ObjectUtils.isEmpty(keyList)){
return;
}
//创建workbook,SXSSFWorkbook支持大数据量导出
Workbook workbook = new SXSSFWorkbook();
//创建sheet
Sheet sheet = workbook.createSheet(excelHeader.sheetName());
//创建表头
Row row = createHeader(workbook, sheet, excelHeader, excelFieldList);
//写入数据
setData(data, sheet, keyList, excelFieldList);
//输出文件
try {
String fileName = URLEncoder.encode(excelHeader.fileName(), "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes("ISO8859-1")));
response.setHeader("filename", fileName);
workbook.write(response.getOutputStream());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* @description: 设置数据
* @create: 2023/9/13 9:33
* @param data
* @param sheet
* @param keyList
* @return void
*/
private static void setData(List data, Sheet sheet, List keyList, List excelFieldList){
List
Test.java
@Data
@ExcelHeader(fileName = "导出文件.xlsx")
public class Test {
@ExcelField(name = "字段1", width = 500, order = 1)
private String field1 = "撒大声地撒分都是";
@ExcelField(name = "字段2", width = 400, order = 2)
private Integer field2 = 11;
@ExcelField(name = "字段3", width = 300, order = 5)
private Double field3;
@ExcelField(name = "字段4", width = 700, order = 10)
private BigDecimal field4 = new BigDecimal("12312312312.222");
@ExcelField(name = "字段5", width = 600, order = 3)
private Date field5 = new Date();
}
TestController.java
@Api(tags = "测试")
@RestController
@RequestMapping("/test")
public class TestController {
@ApiOperation("test")
@PostMapping("/test")
public void test(HttpServletResponse response){
Test test = new Test();
List list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add(test);
}
ExcelExportUtil.export(response, list, Test.class);
}
}
输出结果