java可选字段导出excel 转换字典值

可直接使用

基础功能跳转此文章 [原出处]
https://blog.csdn.net/weixin_44736584/article/details/109457982?spm=1001.2014.3001.5502

上代码

依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
// 如果是高版本 会有可能出错

excel注解

package com.ls.utils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@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 "";
}

excel导出工具类


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 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(Excel.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(Excel.class).name());
            System.out.println(styles.get("header"));
            cell.setCellStyle(styles.get("header"));
        }
    }
 
    /**
     * 转换字典值
     *     将数据中字典值转化为对应的值(注:字典值应为String格式)
     */
    private void convertDict() throws IllegalAccessException {
        for (Field field : fieldsContainDict) {
            Excel annotation = field.getAnnotation(Excel.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
        for (Field declaredField : clazz.getDeclaredFields()) {
            if(declaredField.getAnnotation(Excel.class) != null) {
                System.out.println(declaredField.getAnnotation(Excel.class).dictExp());
            }
        }
        this.fieldsContainDict = Arrays
                .asList(clazz.getDeclaredFields())
                .stream()
                .filter(item -> !"".equals(item.getAnnotation(Excel.class) != null? 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";
    }
}

在实体类加注解

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {

    @Excel(name = "部门编号")
    private Integer deptno;

    @Excel(name = "部门名称")
    private String dname;

    @Excel(name = "加入时间",dateFormat = "yyyy-MM-dd")
    private Date joinTime;

    @Excel(name = "性别", dictKey = "sex", dictExp = "1=男,2=女")
    private String sex;
}
}

导出测试
导出全部字段 日期格式化 字典值转换

public class Demo1 {
    public static void main(String[] args) throws IOException {

        // 准备数据
        List<Dept> depts = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Dept dept = new Dept();
            dept.setDname("财务部"+i);
            dept.setDeptno(12+i);
            dept.setJoinTime(new Date());
            dept.setSex("1");
            depts.add(dept);
        }
        // 导出excel
        ExcelUtil<Dept> deptExcelUtil = new ExcelUtil<>(Dept.class);
        deptExcelUtil.exportExcel(depts);
    }
}

结果图例
注:成功后会在d盘出现 一个xlsx类型的excel文件
在这里插入图片描述
接下来是可选字段导出

public static void main(String[] args) {
        List<Dept> depts = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Dept dept = new Dept();
            dept.setDname("财务部"+i);
            dept.setDeptno(12+i);
            dept.setJoinTime(new Date());
            dept.setSex("1");
            depts.add(dept);
        }
        // 封装一个map 把想要导出的字段名称放入到  map的key即可 注:导出字段名要和实体类保持一致
        Map<String, Object> map = new HashMap<>();
        map.put("deptno",null);
        map.put("dname",null);
        // 将fieldsName放入方法中
        ExcelUtil<Dept> deptExcelUtil = new ExcelUtil<>(Dept.class);
        deptExcelUtil.exportExcel(depts);
    }

结果实例
注:成功后会在d盘出现 一个xlsx类型的excel文件
在这里插入图片描述
现在来完成完整下载
在上述 ExcelUtil 工具类中 找到 下图这个方法
在这里插入图片描述
参数加 HttpServletResponse

public void exportExcel(List<T> list, Map<String, Object> fieldsName, String sheetName, HttpServletResponse res){}

上面截图这一块
在这里插入图片描述

可换成为

在这里插入图片描述
这样就可以访问接口下载了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值