原文:https://www.jb51.net/article/186910.htm
maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!--lombok可选-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
自定义注解类
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Excel {
/**
* 表头
* @return
*/
String value() default "";
/**
* 列索引
* @return
*/
int columnIndex() default 0;
}
实体类
在需要导出的字段上加入自定义的注解
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Users {
private int id;
@Excel(value = "用户名")
private String userName;
@Excel(value = "密码")
private String passWord;
}
工具类
public class ExcelUtil {
public static void main(String[] args) {
try {
contextLoads();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 导出数据到Excel
* @throws IOException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void contextLoads() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
IllegalAccessException {
// 模拟数据
Users user1 = new Users(1, "11111", "1111");
Users user2 = new Users(1, "22222", "22222");
List<Users> uList = Arrays.asList(user1, user2);
// 文件路径与文件名(.xlsx请用XSSF替换HSSF)
String path = "C:/Users/Administrator/Desktop/";
String fileName = "test.xls";
// 将数据写入excel,设置页签名称
HSSFWorkbook workbook = exportExcel(uList, Users.class);
workbook.setSheetName(0, "sheetName");
FileOutputStream fileOutputStream = new FileOutputStream(path + fileName);
workbook.write(fileOutputStream);
}
/**
* 数据处理
* @param <T>
* @param data 导出的数据
* @param clz 实体类
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static <T> HSSFWorkbook exportExcel(List<T> data, Class<T> clz)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// 反射获取class所有声明的方法
Field[] Fields = clz.getDeclaredFields();
LinkedList<String> variables = new LinkedList<>();
// 创建工作薄对象
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
// 创建工作表对象
HSSFSheet sheet = hssfWorkbook.createSheet();
// 创建表头
HSSFRow rowHeader = sheet.createRow(0);
// 表头下标
int index = 0;
// 从@Excel注解读取表头
for (int i = 0; i < Fields.length; i++) {
Field field = Fields[i];
// 判断是否存在@Excel注释
if (field.isAnnotationPresent(Excel.class)) {
// 获取当前字段的@Excel信息并填充到单元格
Excel annotation = field.getAnnotation(Excel.class);
rowHeader.createCell(index).setCellValue(annotation.value());
variables.add(field.getName());
index++;
}
}
// 数据处理
for (int i = 0; i < data.size(); i++) {
// 创建工作表的行(表头占第一行)
HSSFRow row = sheet.createRow(i + 1);
// 获取一行数据
T t = data.get(i);
Class<? extends Object> aClass = t.getClass();
// 填充数据
for (int j = 0; j < variables.size(); j++) {
// 反射获取variables.get(j)字段
Field declaredField = aClass.getDeclaredField(variables.get(j));
// 设置字段可访问
declaredField.setAccessible(true);
// 获取当前字段值并填充数据
Object object = declaredField.get(t);
row.createCell(j).setCellValue(object.toString());
}
}
return hssfWorkbook;
}
}
结果