easyexcel使用
https://www.yuque.com/easyexcel/doc/write#xuM8l这里面写的非常清楚
@Data
@ApiModel("导出Excel")
@HeadStyle(fillForegroundColor = 22)
@HeadFontStyle(fontHeightInPoints = 10)
@ColumnWidth(value = 13) // 列宽
public class DetailResultExcelVo {
@ExcelProperty(value = {"保险","规则"},converter = BaseRoundRuleConverter.class)
@ApiModelProperty(value = "规则")
private Byte baseRoundRule;
}
try { //"Content-Disposition","attachment;这个是说页面自动下载 response.addHeader("Content-Disposition","attachment; filename="+ java.net.URLEncoder.encode(name, "UTF-8")); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(name, StandardCharsets.UTF_8); // 这里需要设置不关闭流 EasyExcel.write(response.getOutputStream(), clazz).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("Sheet") .doWrite(list); EasyExcel.write(name, clazz).sheet().doWrite(list); } catch (Exception e) { //ResultData.error("下载文件失败"); response.getWriter().println(JsonUtils.toJson(resultData)); }
//处理当数据为-1时Excel显示空单元格 public class SpecialDataConverter implements Converter<Number> { @Override public Class supportJavaTypeKey() { return null; } @Override public CellDataTypeEnum supportExcelTypeKey() { return null; } @Override public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { return null; } @Override public CellData convertToExcelData(Number value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { //当传过来的value是Integer,value.equals(-1)生效,发现value是Long型不生效,所以加上了value.longValue()==-1 //new CellData(value),value是Number类型报错,所以转为value.toString() if(value.equals(-1)||value.longValue()==-1){ return new CellData(""); } return new CellData(value.toString()); } }
//Converter例子
@Getter @ToString @ApiModel("转换器") public class RuleConverter implements Converter<Byte> { @Override public Class supportJavaTypeKey() { return null; } @Override public CellDataTypeEnum supportExcelTypeKey() { return null; } @Override public Byte convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { return null; } @Override public CellData convertToExcelData(Byte aByte, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception { String name = RuleEnum.getNameByCode(aByte); return new CellData(name); } }
posted on 2022-02-23 10:09 sunshineicy 阅读(492) 评论(0) 收藏 举报
浙公网安备 33010602011771号