hutool的ExcelUtil导出excel


     JwfsStdPopRealPopNewTemplateVo jwfsStdPopRealPopNewTemplateVo = JwfsStdPopRealPopNewTemplateVo.builder().build();
        export(
                data,
                "C:\\Users\\Administrator\\Desktop\\1\\aa\\",
                "aa1",
                jwfsStdPopRealPopNewTemplateVo
        );

   public static void export(List<JwfsStdPopRealPopNewTemplateVo> data, String path, String fileName, JwfsStdPopRealPopNewTemplateVo excelData) throws IOException {
        log.info("开始导出");
        ExcelWriter writer = ExcelUtil.getWriter();
        List<Map> fieldAnnotation = getFieldAnnotation(excelData);
        for(Map<String,String> map:fieldAnnotation){
            for(String key:map.keySet()){
                String value = map.get(key);
                writer.addHeaderAlias(key,value);
            }
        }

        //对齐方式,水平左对齐,垂直中间对齐
        writer.getStyleSet().setAlign(HorizontalAlignment.LEFT, VerticalAlignment.CENTER);

        //标题样式
        CellStyle headCellStyle = writer.getHeadCellStyle();
        //设置背景色
        headCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
        //必须设置 否则背景色不生效
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //创建标题字体
        Font headFont = writer.createFont();
        headFont.setFontName("宋体");
        //大小
        headFont.setFontHeightInPoints((short) 11);
        //加粗
        headFont.setBold(true);
        headCellStyle.setFont(headFont);

        writer.setColumnWidth(-1, 30);
        writer.setRowHeight(-1,20);


        //保存路径
        FileOutputStream output = new FileOutputStream(path+fileName+".xls");
        // 一次性写出内容
        writer.write(data);

        //自适应列宽
        int columnCount = writer.getColumnCount();
        for (int i = 0; i < columnCount; ++i) {
            double width = SheetUtil.getColumnWidth(writer.getSheet(), i, false);
            if (width != -1.0D) {
                width *= 256.0D;
                //此处可以适当调整,调整列空白处宽度
                width += 220D;
                writer.setColumnWidth(i, Math.toIntExact(Math.round(width / 256D)));
            }
        }

        writer.flush(output,true);
        // 关闭writer,释放内存
        writer.close();
    }
    public static List<Map> getFieldAnnotation(Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        Map<String, String> resultMap = new HashMap();
        List<Map> list=new ArrayList<>();
        for (Field field : fields) {
            // 是否引用ApiModelProperty注解
            boolean bool = field.isAnnotationPresent(ApiModelProperty.class);
            if (bool) {
                resultMap = new HashMap();
                String value = field.getAnnotation(ApiModelProperty.class).value();
                resultMap.put(field.getName(), value);
                list.add(resultMap);
            }
        }
        return list;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值