• 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); } }
  • 相关阅读:
    IntelliJ IDEA 2019.2.3 x64版本设置Run Dashboard
    IntelliJ IDEA 使用Tomcat后在控制台出现乱码'中文乱码 “淇℃伅”“涓夋湀”
    window10家庭版管理员权限问题
    用navicat连接数据库报错:1130-host ... is not allowed to connect to this MySql server如何处理
    IntelliJ IDEA导入已有的项目的方法
    从养孩子谈谈 IO 模型(一)
    写作之路,以梦为马,不负韶华
    数据库核心:索引,你知道多少?
    面试:啥是数据倾斜?就是数据歪啦!
    ​面试:业务开发中你用到了哪些算法(续)?
  • 原文地址:https://www.cnblogs.com/jingRegina/p/15926076.html
Copyright © 2020-2023  润新知