EasyExcel:https://www.yuque.com/easyexcel/doc/easyexcel
实体类常用注解
@ContentRowHeight(20)
主体内容行高
@HeadRowHeight(20)
表头行高
@HeadFontStyle(fontName = “微软雅黑”,bold = false,fontHeightInPoints =11)
表头文字样式
@HeadStyle(fillBackgroundColor = 52)
表头样式
@ColumnWidth(10)
主题内容行宽
@ExcelIgnore
忽略此行
@ExcelProperty(“任务编号”)
写入此行
!!需要注意的地方
@ExcelProperty(value = "开始时间",converter = LocalDateTimeConverter.class)
private LocalDateTime beginTime;
时间类型最好这样写 不然会报错 说找不到转换类
↓是LocalDateTimeConverter
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* 时间格式的转换器 用于写入excel
*/
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"));
}
@Override
public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
test
@Test
void testExcel() throws IOException {
String fileName = "C:\\Users\\22735\\Desktop\\"+"EasyExcelWrite.xlsx";
// 文件名 格式类 表名 数据
EasyExcel.write(fileName, InspectionDetailDoneVo.class).sheet("工作表名").doWrite(dataService.findAllInspectionDoneDetail(new InspectionDetailDoneDto()));
System.out.println("finished");
}
官方文档尚不完善