easyexcel使用
https://www.yuque.com/easyexcel/doc/write#xuM8l这里面写的非常清楚
1 2 3 4 5 6 7 8 9 10 | @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 阅读(464) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本