1、用于导出功能的注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Excel {
/**
* 表格名称
*/
String name();
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelItem {
/**
* 表格列的名称
*/
String name();
/**
* 表格列的顺序
*/
int order();
/**
* 表格列的长度
*/
int length() default 8;
}
2、自定义导出数据异常类
/**
* 自定义导出数据异常
*/
public class ExcelException extends Exception {
/**
* 无参构造
*/
public ExcelException(){
super();
}
/**
* 带参构造
* @param message
*/
public ExcelException(String message){
super(message);
}
}
3、相关实体类
/**
* 导出表格列名实体类
*/
public class ExcelColumn {
private String value; // 值
private int order; // 顺序
private int length; // 列宽
......
}
/**
* 导出表格数据实体类
*/
public class ExcelData {
private String value; // 值
private int order; // 顺序
}
4、导出数据工具类
public class ExcelUtil {
/**
* 工作簿
*/
private static Workbook workbook;
/**
* 表格
*/
private static Sheet sheet;
/**
* 列名集合
*/
private static List<ExcelColumn> columns;
/**
* 初始化工作簿
*/
private static void initWorkbook(){
workbook = new XSSFWorkbook();
}
/**
* 初始化表格
* @param sheetName 表格名称
*/
private static void initSheet(String sheetName){
sheet = workbook.createSheet(sheetName);
}
/**
* 初始化标题
* @param title 表格标题
*/
private static void initTitle(String title){
System.out.println("初始化标题");
}
/**
* 初始化列名
* @param target
*/
private static void initColumns(Class<?> target){
// 创建列名信息对象集合
columns = new ArrayList<>();
// 定义列名信息对象
ExcelColumn excelColumn;
// 获取列名信息对象的属性
Field[] fields = target.getDeclaredFields();
// 遍历属性
for (Field field : fields) {
field.setAccessible(true);
// 获取属性的ExcelItem注解对象
ExcelItem item = field.getAnnotation(ExcelItem.class);
// 存在ExcelItem注解
if (item != null) {
// 创建列名信息对象
excelColumn = new ExcelColumn();
// 设置列名信息
excelColumn.setOrder(item.order());
excelColumn.setValue(item.name());
excelColumn.setLength(item.length());
// 添加列名信息对象
columns.add(excelColumn);
}
}
}
/**
* 导出单行数据
* @param datas
* @param row
*/
private static void excelRow(List<ExcelData> datas, Row row){
// 遍历数据
for (ExcelData data : datas) {
Cell cell = row.createCell(data.getOrder());
cell.setCellValue(data.getValue());
}
}
/**
* 导出数据
* @param target
* @param collection
* @param sheetName
*/
public static void excel(Class<?> target, Collection<?> collection, String sheetName) throws Exception {
if (collection == null || collection.isEmpty()) {
return;
}
if (!target.isAnnotationPresent(Excel.class)) {
throw new ExcelException("Class " + target.getName() + " is not annotated with Excel annotation");
}
Class<?> origin = collection.iterator().next().getClass();
if (!target.equals(origin)) {
throw new ExcelException("original class is not equals with target class");
}
// 初始化操作
initWorkbook();
initSheet(sheetName);
initColumns(target);
// 定义变量rowNum记录表格行数
int rowNum = 0;
// 定义变量row
Row row;
//
row = sheet.createRow(rowNum++);
// 列名数据
for (ExcelColumn column : columns) {
sheet.setColumnWidth(column.getOrder(), column.getLength() * 256);
Cell cell = row.createCell(column.getOrder());
cell.setCellValue(column.getValue());
}
// 表格单行数据
List<ExcelData> datas;
// 单元格数据
ExcelData data;
// 迭代数据
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
//
datas = new ArrayList<>();
// 获取数据对象
Object object = iterator.next();
// 获取数据对象的属性
Field[] fields = target.getDeclaredFields();
// 遍历属性
for (Field field : fields) {
// 设置强制访问
field.setAccessible(true);
// 获取数据对象属性的ExcelItem注解对象
ExcelItem item = field.getAnnotation(ExcelItem.class);
// 存在ExcelItem注解
if (item != null) {
data = new ExcelData();
data.setOrder(item.order());
data.setValue(String.valueOf(field.get(object)));
datas.add(data);
}
}
row = sheet.createRow(rowNum++);
// 写入表格一行数据
excelRow(datas, row);
}
// 输出流
FileOutputStream outputStream = new FileOutputStream(new File(UUID.randomUUID().toString() + ".xlsx"));
// 写入表格
workbook.write(outputStream);
// 关闭
workbook.close();
outputStream.close();
}
}
5、测试
@Excel(name = "用户信息表格")
public class User {
@ExcelItem(name = "ID编号", order = 0, length = 16)
private String id;
@ExcelItem(name = "姓名", order = 1, length = 16)
private String name;
@ExcelItem(name = "性别", order = 2, length = 16)
private Sex sex;
@ExcelItem(name = "年龄", order = 3, length = 16)
private int age;
@ExcelItem(name = "电话", order = 4, length = 16)
private String phone;
@ExcelItem(name = "邮箱", order = 5, length = 16)
private String email;
@ExcelItem(name = "地址", order = 6, length = 16)
private String address;
}
6、测试结果