1:先定义excel导出的注解
package com.example.demo.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Exceld {
public String name() default "";
}
2:创建对应实体类
package com.example.demo.Main;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.example.demo.Annotation.Exceld;
//EasyExcl学生表
public class Student {
public Student(){
}
public Student(Integer id,String name,Integer age){
this.id=id;
this.name=name;
this.age=age;
}
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@Exceld(name="姓名")
private String name;
@Exceld(name="年龄")
private Integer age;
@Exceld(name="生日")
private Date date;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
3:核心代码一:解析传入的list为List<List<Object>>只对时间类型进行yyyy-MM-dd进行转化,可以自行根据情况进行调整
public static List<List<Object>> getList(List<?> list) throws IllegalArgumentException, IllegalAccessException {
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
List<List<Object>> objlist = new ArrayList<>();
List<Object> obj0 = new ArrayList<>();
for (Field field : list.get(0).getClass().getDeclaredFields()) {
Exceld e = field.getAnnotation(Exceld.class);
if (e == null) {
continue;
}
obj0.add(e == null ? "" : e.name());
}
objlist.add(obj0);
Iterator<?> iter = list.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
final List<Object> obj1 = new ArrayList<>();
for (Field field : obj.getClass().getDeclaredFields()) {
field.setAccessible(true);
Exceld e = field.getAnnotation(Exceld.class);
if (e == null) {
continue;
}
Object Value = field.get(obj);
if(field.getType()==Date.class&&Value!=null){
Value=df.format((Date)Value);
}
Value = Value == null ? "" : Value;
obj1.add(Value);
}
objlist.add(obj1);
}
return objlist;
}
4:创建excl ,LIst<List<Object>插入excl表中,样式可以根据需求自己调整
public static CellStyle getCellStyleTitle(HSSFWorkbook workbook){
CellStyle cellStyleTitle = workbook.createCellStyle();
HSSFFont fontTitle = workbook.createFont();
fontTitle.setFontHeightInPoints((short) 25); //字体大小
fontTitle.setFontName("宋体"); //字体
fontTitle.setBold(true);
cellStyleTitle.setFont(fontTitle);
cellStyleTitle.setAlignment(HorizontalAlignment.CENTER); //水平布局:居中
cellStyleTitle.setWrapText(true);//设置自动换行
return cellStyleTitle;
}
public static CellStyle getCellStyle(HSSFWorkbook workbook){
CellStyle cellStyle= workbook.createCellStyle();
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 15); //字体大小
font.setFontName("宋体"); //字体
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.LIME.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
return cellStyle;
}
public static HSSFWorkbook oupt1(List<?> list) throws Exception {
HSSFWorkbook workbook = new HSSFWorkbook();
CellStyle cellStyleTitle=getCellStyleTitle(workbook); //获取大标题样式
CellStyle cellStyle=getCellStyle(workbook); //获取小标题样式
HSSFSheet sheet = workbook.createSheet("学生表");
List<List<Object>> lists = getList(list);
int hb=lists.get(0).size()-1;
CellRangeAddress region = new CellRangeAddress(0, 0, 0, hb);
Row rowtitle = sheet.createRow(0);
rowtitle.createCell(0).setCellValue("学生表");
rowtitle.getCell(0).setCellStyle(cellStyleTitle);
sheet.addMergedRegion(region);
for (int i = 0; i < lists.size(); i++) {
int line=i+1;
Row row = sheet.createRow(line);
for (int j = 0; j < lists.get(i).size(); j++) {
row.createCell(j).setCellValue(lists.get(i).get(j).toString());
if(i==0){
System.out.println(lists.get(i).get(j).toString());
row.getCell(j).setCellStyle(cellStyle);
}
}
}
return workbook;
}
5:最后一步运行Main方法导出
public static void main(String[] args) throws Exception {
Student stu = new Student(1, "张三", 33);
Student stu1 = new Student(2, "李四", 22);
Student stu2 = new Student(3, null, 33);
Student stu3 = new Student(4, "孙六", 24);
stu.setDate(new Date());
List<Student> stus = new ArrayList<Student>();
stus.add(stu);
stus.add(stu1);
stus.add(stu2);
stus.add(stu3);
//getField(stus);
HSSFWorkbook work = oupt1(stus);
work.write(new File("d:/x.xls"));
}