工具类:
public class ExcelUtil {
/**
* @ClassName ExcelUtil
* @Description : 用于生成指定目录的Excel不用ReisterConverter
*/
public static boolean generatorExcelWithoutConverter(String path, Class T, String sheetName, List<? extends Object> res){
try{
EasyExcel.write(path, T).sheet(sheetName).doWrite(res);
}
catch (Exception e){
System.out.println(e.getMessage());
return false;
}
return true;
}
/**
* @ClassName ExcelUtil
* @Description : 用于生成指定目录的Excel使用指定的ReisterConverter
*/
public static boolean generatorExcelWithConverter(String path, Class T, String sheetName, List<? extends Object> res, Converter converter){
try{
EasyExcel.write(path, T).registerConverter(converter).sheet(sheetName).doWrite(res);
}
catch (Exception e){
System.out.println(e.getMessage());
return false;
}
return true;
}
}
实体类:
@Accessors(chain = true)
public class ExcelByDay {
@ExcelIgnore
private static final long serialVersionUID = 1L;
/**
* 员工编号
*/
@ExcelProperty(value = "员工编号",index = 0)
private Integer employeeId;
/**
* 职工姓名
*/
@ExcelProperty(value = "职工姓名",index = 1)
private String employeeName;
/**
* 工时
*/
@ColumnWidth(15)
@ExcelProperty(value = "工时",index = 15,converter = LocalDateTimeLastConverter.class)
private Double workHours;
@ColumnWidth(15)是excal的宽度
value就是属性名,
index是次序,
converter = LocalDateTimeLastConverter.class)这是时间的特殊处理,需要下方处理类:
LocalDateTime
public class LocalDateTimeConverter implements Converter<LocalDateTime> {
@Override
public Class<LocalDateTime> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
@Override
public CellData convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}
}
LocalDate
public class LocalDateConverter implements Converter<LocalDate> {
@Override
public Class<LocalDate> supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
@Override
public CellData convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
LocalTime
public class LocalTimeConverter implements Converter<LocalTime> {
@Override
public Class<LocalTime> supportJavaTypeKey() {
return LocalTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return LocalTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("HH:mm:ss"));
}
@Override
public CellData convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return new CellData<>(value.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
}
}
最终使用:
/**
* 生成报表
* @param days 时间
* @param format 报表前缀
* @param path 报表路径
*/
private void ExcelDownload(int days,String format,String path,int sign){
//前一周的时间
LocalDate end = LocalDate.now().minusDays(1);
LocalDate start =end.minusDays(days);
//文件名
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String filename = format +dateTimeFormatter.format(start) +".xlsx";
//跟文件路径
String filepath = System.getProperty("user.dir")+path+filename;
//一周的时间列表
List<LocalDate> timeList=new ArrayList<>();
for (int i = 0; i < days; i++) {
timeList.add(end.minusDays(i));
}
//查询出当前所有记录
List<ExcelByDay> list = listExcelByList(timeList);
System.out.println("----------------------------------"+list);
//查询表中有没有
//生成excel,先不用线程
ExcelUtil.generatorExcelWithoutConverter(filepath,ExcelByDay.class,filename,list);
}