做法是用注解加暴力反射做的
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author:sean
* @date:2018/10/18
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcelSequence {
/**
* 字段名称 excel的列名
* @return
*/
String name() default "";
/**
* 排序字段 Excel的列的位置
* @return
*/
int index();
}
实体类
import com.kuaihuoyun.ttms.web.annotations.ExcelSequence;
import lombok.Data;
import java.io.Serializable;
/**
* @author: sean
*/
public class OrderTargetAllExportVO implements Serializable {
/**
*总订单数
*/
@ExcelSequence(name = "订单总数", index = 0)
private Integer totalOrderNumber;
/**
*签收数
*/
@ExcelSequence(name = "订单签收数", index = 1)
private Integer signOrder;
}
工具类:
import com.kuaihuoyun.ttms.web.annotations.ExcelSequence;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author: guoshuyun
*/
public class ExportExcelUtil {
public static <T> SXSSFWorkbook exportExcel(List<T> list, Class clas,String header) throws IllegalAccessException,InvocationTargetException,NoSuchMethodException {
SXSSFWorkbook book = new SXSSFWorkbook(); ;
Field[] fields = clas.getDeclaredFields();
Map<Integer, String> nameMap = new HashMap<>(fields.length);
Map<Integer, String> fieldNameMap = new HashMap<>(fields.length);
for (Field f : fields) {
ExcelSequence annotation = f.getAnnotation(ExcelSequence.class);
if (annotation != null){
String name = "".equals(annotation.name()) ? f.getName() : annotation.name();
Integer order = annotation.index();
nameMap.put(order,name);
fieldNameMap.put(order,f.getName());
}
}
Sheet sheet = book.createSheet();
Row row0 = sheet.createRow(0);
sheet.addMergedRegion(new CellRangeAddress(0,0,0,nameMap.size()-1));
CellStyle cellStyle = book.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
Cell cell1 = row0.createCell(0);
//header是表头,上面是合并单元格,可以去看一下API
cell1.setCellValue(header);
cell1.setCellStyle(cellStyle);
Row row3 = sheet.createRow(1);
for (int i = 0; i < nameMap.size(); i++) {
row3.createCell(i).setCellValue(nameMap.get(i));
}
for (int i = 0; i < list.size(); i++) {
Row row = sheet.createRow(i + 2);
T t = list.get(i);
for(Map.Entry<Integer,String> entry : fieldNameMap.entrySet()){
Integer index = entry.getKey();
Cell cell = row.createCell(index);
String fieldName = fieldNameMap.get(index);
String methodName = "get"+ fieldName.substring(0,1).toUpperCase() + fieldName.substring(1);
Method method = clas.getMethod(methodName,new Class[0]);
Optional<Object> cellValue = Optional.ofNullable(method.invoke(t,new Object[0]));
String fieldValue = cellValue.orElse("--").toString();
cell.setCellValue(fieldValue);
}
}
return book;
}
}
望各位看官觉得有不对的地方还望指出,这个比较适合固定格式导出的场景。