OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3219|回复: 0

java实现数据的Excel导出, 自定义导出字段, 转换字典值

[复制链接]

143

主题

145

帖子

0

精华

高级会员

Rank: 4

积分
585
金钱
585
注册时间
2020-5-25
在线时间
42 小时
发表于 2020-11-4 16:54:06 | 显示全部楼层 |阅读模式

第一版代码:
基础功能跳转此文章java自定义Excel导出工具:

简介

新增功能:

  • 添加自定义字段导出功能, 用户可以选择字段进行导出
  • 将字典类型数据进行转换(如:0=女,1=男, 将0转换为女, 1转换为男)
  • 添加表头格式
  • 随机文件名称, 防止多次导出时文件覆盖问题
实现代码

Excel注解

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface Excel {    /**     * 导出到Excel中的名字     */    String name() default "";    /**     * 日期格式, 如: yyyy-MM-dd     */    String dateFormat() default "";    /**     * 字典的key值     */    String dictKey() default "";    /**     * 读取内容转表达式 (如: 0=男,1=女,2=未知)     */    String dictExp() default "";}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Excel的导出工具类

/** * Excel的工具类 */public class ExcelUtil<T> {    /**     * 工作薄     */    private Workbook wb;    /**     * 工作表     */    private Sheet sheet;    /**     * 需要导出的数据     */    private List<T> exportList;    /**     * 对象的class对象     */    private Class<T> clazz;    /**     * 被选中需要导出的字段名称     */    private Map<String, Object> checkedFieldsName;    /**     * 被选中需要导出的字段对象     */    private List<Field> checkedFields;    /**     * 包含需要字典转换的字段对象     */    private List<Field> fieldsContainDict;    /**     * 对象中的字典值     */    private Map<String, Map<String, String>> dicts;    private ExcelUtil(){    }    public ExcelUtil(Class<T> clazz){        this.clazz = clazz;    }    /**     *     * @param list     * @param sheetName     * @param fieldsName     */    public void exportExcel(List<T> list, Map<String, Object> fieldsName, String sheetName){        // 初始化数据        init(list, sheetName, fieldsName);        // 转换字典值        try {            convertDict();        } catch (IllegalAccessException e) {            e.printStackTrace();        }        // sheet第一行加入名称数据        createTopRow();        // sheet其他行,添加目标数据        try {            createOtherRow();        } catch (IllegalAccessException e) {            e.printStackTrace();        }        // 导出wb        try(OutputStream outFile = new FileOutputStream(generateFileName())){            wb.write(outFile);        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                wb.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    /**     * 添加导出数据     */    private void createOtherRow() throws IllegalAccessException {        for (int rowNum = 1; rowNum <= exportList.size(); rowNum++) {            Row row = sheet.createRow(rowNum);            T t = exportList.get(rowNum - 1);            for (int colNum = 0; colNum < checkedFields.size(); colNum++) {                Cell cell = row.createCell(colNum);                Field field = checkedFields.get(colNum);                field.setAccessible(true);                // 单元格设置值                addCell(cell, field, t);            }        }    }    /**     * 单元格中添加数据     *     * @param cell  单元格     * @param field 字段     * @param t     list中的一条数据     */    private void addCell(Cell cell, Field field, T t) throws IllegalAccessException {        Class<?> fieldType = field.getType();        if (String.class == fieldType) {            cell.setCellValue((String) field.get(t));        } else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {            cell.setCellValue((Integer) field.get(t));        } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {            cell.setCellValue((Long) field.get(t));        } else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {            cell.setCellValue((Double) field.get(t));        } else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {            cell.setCellValue((Float) field.get(t));        } else if (Date.class == fieldType) {            String dateFormat = field.getAnnotation(Excels.class).dateFormat();            cell.setCellValue(dateFormat((Date) field.get(t), dateFormat));        }    }    /**     * 时间格式转换     * @param date 日期     * @param dateFormat 日期格式     * @return     */    private String dateFormat(Date date, String dateFormat) {        if (dateFormat == null || "".equals(dateFormat)) {            dateFormat = "yyyy-MM-dd HH:mm:ss";        }        SimpleDateFormat df = new SimpleDateFormat(dateFormat);        return df.format(date);    }    /**     * sheet第一行加入名称数据     */    private void createTopRow() {        Row row = sheet.createRow(0);        Map<String, CellStyle> styles = createStyles(wb);        for (int index = 0; index < checkedFields.size(); index++) {            Cell cell = row.createCell(index);            cell.setCellValue(checkedFields.get(index).getAnnotation(Excels.class).name());            System.out.println(styles.get("header"));            cell.setCellStyle(styles.get("header"));        }    }    /**     * 转换字典值     *     将数据中字典值转化为对应的值(注:字典值应为String格式)     */    private void convertDict() throws IllegalAccessException {        for (Field field : fieldsContainDict) {            Excels annotation = field.getAnnotation(Excels.class);            String dictKey = annotation.dictKey();            field.setAccessible(true);            for (T t : exportList) {                // 获取字段值                String o = (String) field.get(t);                field.set(t, dicts.get(dictKey).get(o));            }        }    }    /**     * 将数据导出Excel     *     * @param list 需要导出的数据     * @param sheetName 工作表名称     */    public void exportExcel(List<T> list, String sheetName){        exportExcel(list, null, sheetName);    }    /**     * 将数据导出Excel     *     * @param list 需要导出的数据     */    public void exportExcel(List<T> list) {        exportExcel(list, null, "sheet");    }    /**     * 初始化     */    public void init(List<T> list ,String sheetName,  Map<String, Object> fieldsName){        this.checkedFieldsName = fieldsName;        this.exportList = list;        // 初始化导出数据        initExportList();        // 初始化工作薄        initWorkbook();        // 初始化工作表        initSheet(sheetName);        // 初始化checkedFields, fieldsContainDict        initFields();        // 根据注解生成生成字典        generateObjDict();    }    /**     * 初始化导出数据     */    private void initExportList(){        // 防止导出过程中出现空指针        if(Objects.isNull(this.exportList)) {            this.exportList = new ArrayList<>();        }    }    /**     * 初始化工作簿     */    private void initWorkbook(){        this.wb = new SXSSFWorkbook();    }    /**     * 初始化工作表     */    private void initSheet(String sheetName){        this.sheet = wb.createSheet(sheetName);    }    /**     * 初始化checkedFields, fieldsContainDict     *     fieldsContainDict含有字典表达式的字段     *     checkedFields用户选中的字段     *        1.如果checkedFieldsName没有定义(未自定义导出字段),所有字段全部导出     *        2.如果checkedFieldsName进行了定义,根据定义字段进行导出     */    private void initFields(){        // 获取对象所有字段对象        Field[ fields = clazz.getDeclaredFields();        // 过滤出checkedFields         this.checkedFields = Arrays.                asList(fields).                stream().                filter(item -> {                    if(!Objects.isNull(this.checkedFieldsName)) {                        if (item.isAnnotationPresent(Excel.class)) {                            return checkedFieldsName.containsKey(item.getName());                        }                    } else {                        return item.isAnnotationPresent(Excel.class);                    }                    return false;                })                .collect(Collectors.toList());        // 过滤出fieldsContainDict        this.fieldsContainDict = Arrays                .asList(clazz.getDeclaredFields())                .stream()                .filter(item -> !"".equals(item.getAnnotation(Excel.class).dictExp()))                .collect(Collectors.toList(    }    /**     * 通过扫描字段注解生成字典数据     */    private void generateObjDict(){        if(fieldsContainDict.size() == 0) {            return;        }        if(dicts == null) {            dicts = new HashMap<>(); //  Map<String, List<Map<String, String>>>        }        for (Field field : fieldsContainDict) {            String dictKey = field.getAnnotation(Excel.class).dictKey();            String exps = field.getAnnotation(Excel.class).dictExp();            String[ exp = exps.split(",");            Map<String, String> keyV = new HashMap<>();            dicts.put(dictKey, keyV);            for (String s : exp) {                String[ out = s.split("=");                keyV.put(out[0, out[1);            }           System.out.println("字典值:"+ dicts);        }    }    /**     * 创建表格样式     *     * @param wb 工作薄对象     * @return 样式列表     */    private Map<String, CellStyle> createStyles(Workbook wb)    {        Map<String, CellStyle> styles = new HashMap<String, CellStyle>();        // 数据格式        CellStyle style = wb.createCellStyle();        style.setAlignment(HorizontalAlignment.CENTER);        style.setVerticalAlignment(VerticalAlignment.CENTER);        style.setBorderRight(BorderStyle.THIN);        style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());        style.setBorderLeft(BorderStyle.THIN);        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());        style.setBorderTop(BorderStyle.THIN);        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());        style.setBorderBottom(BorderStyle.THIN);        style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());        Font dataFont = wb.createFont();        dataFont.setFontName("Arial");        dataFont.setFontHeightInPoints((short) 10);        style.setFont(dataFont);        styles.put("data", style);        // 表头格式        style = wb.createCellStyle();        style.cloneStyleFrom(styles.get("data"));        style.setAlignment(HorizontalAlignment.CENTER);        style.setVerticalAlignment(VerticalAlignment.CENTER);        style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);        Font headerFont = wb.createFont();        headerFont.setFontName("Arial");        headerFont.setFontHeightInPoints((short) 10);        headerFont.setBold(true);        headerFont.setColor(IndexedColors.WHITE.getIndex());        style.setFont(headerFont);        styles.put("header", style);        return styles;    }    /**     * 生成随机名称,防止文件复写     * @return     */    private String generateFileName(){        return "D:\\" + UUID.randomUUID().toString().replace("-", "") + ".xlsx";    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
使用方法

将对象加上工具类需要的注解:

@Data@NoArgsConstructor@AllArgsConstructorpublic class Student {    @Excel(name = "姓名")    private String name;    @Excel(name = "年龄")    private Integer age;    @Excel(name = "出生日期", dateFormat = "yyyy-MM-dd")    private Date birthday;    @Excel(name = "性别", dictKey = "sex", dictExp = "1=男,2=女")    private String sex;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

测试字典转换

public static void main(String[ args) {    ArrayList<Student> data = new ArrayList<>();    Student student = new Student();    student.setName("tom");    student.setAge(19);    student.setSex("1");    student.setBirthday(new Date());    data.add(student);    ExcelUtil<Student> util = new ExcelUtil<>(Student.class);    util.exportExcel(data, "人员信息表");}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果:


结果可以看出, 已将1转换为男了


测试选择字段导出:
若不自定义导出的字段, 工具将会把所有带有Excel注解的字段进行导出, 如上方所示。
当输入导出字段时,才能根据定义的字段进行导出,实现如下:

public static void main(String[ args) {    ArrayList<Student> data = new ArrayList<>();    Student student = new Student();    student.setName("tom");    student.setAge(19);    student.setSex("1");    student.setBirthday(new Date());    data.add(student);    // 需要导出字段的名称,放入map的key中即可(这里只导出姓名和性别)    Map<String, Object> fieldsName = new HashMap<>();    fieldsName.put("name", null);    fieldsName.put("sex", null);    ExcelUtil<Student> util = new ExcelUtil<>(Student.class);    // 将fieldsName放入方法中    util.exportExcel(data, fieldsName,"人员信息表");}
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-6-9 14:01

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表