1.引入依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
</dependency>
2.撰寫自定義注解
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.FIELD})
@Documented
public @interface ExcelHeader {
String value() default "";
}
3.撰寫工具類
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
public class ExcelUtil<T> {
/**
* 通用生成excel
*
* @param list Excel資料
* @param cls 物體類class
* @param <T> 物體類
*/
public static <T> void generateExcel(List<T> list, Class cls) throws NoSuchMethodException,
InvocationTargetException, IllegalAccessException, IOException {
// 創建Excel物件
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("excel測驗");
// 創建表頭
HSSFRow header = sheet.createRow(0);
// 獲取欄位
Field[] fields = cls.getDeclaredFields();
// Excel 標題集合
List<String> headers = new ArrayList<>();
// 欄位名集合
List<String> methodNames = new ArrayList<>();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
field.setAccessible(true);
if (field.isAnnotationPresent(ExcelHeader.class)) {
// 獲取注解value值 即表頭
String value = field.getAnnotation(ExcelHeader.class).value();
HSSFCell cell = header.createCell(i);
cell.setCellValue(value);
// 獲取get方法
String methodName = "get" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1);
methodNames.add(methodName);
}
}
for (int i = 0; i < list.size(); i++) {
T t = list.get(i);
// 創建行
HSSFRow row = sheet.createRow(i + 1);
for (int j = 0; j < methodNames.size(); j++) {
// 創建列
HSSFCell cell = row.createCell(j);
Method method = null;
method = cls.getMethod(methodNames.get(j));
Object invoke = method.invoke(t);
if (invoke == null) {
cell.setCellValue("");
} else {
cell.setCellValue(invoke.toString());
}
}
}
FileOutputStream fileOut = new FileOutputStream("C:/workbook.xls");
wb.write(fileOut);
}
}
4.測驗物體類
public class Student {
@ExcelHeader("姓名")
private String name;
@ExcelHeader("編號")
private String stuNo;
@ExcelHeader("年齡")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
5.測驗方法
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setName("張三");
student.setStuNo("20210108");
student.setAge("18");
students.add(student);
try {
generateExcel(students,Student.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
6.輸出結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/246825.html
標籤:java
上一篇:旋轉陣列
下一篇:Java基礎09 面向物件~封裝
