SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出

前言

如题,这个小玩意,就是不限制你查的是哪张表,用的是什么类。

我直接一把梭,嘎嘎给你一顿导出。

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第1张图片

我知道,这是很多人都想过的, 至少我就收到很多人问过我这个类似的问题。

我也跟他们说了,但是他们就是不动手,其实真的很简单。

不动手怎么办? 我出手呗。

不多说开搞 。

正文

玩法很简单,我之前有写过一篇利用csv文件内容格式做excel文件导出的。

如果没有看过的,还等什么,现在就去看看:

要实现的效果 :

类是不确定的 ,User ?Student ?  District ?   不确定。

但是呢我们封装出来的函数,要足够支撑不同的类,我们自动去读取遍历list ,然后导出生成文件。

核心的思路是什么 ?

其实就还是利用csv文件的内容格式本质 ,看这两幅图 :

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第2张图片 SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第3张图片

我们要实现万能的类导出excel !!!

思路是什么 :

① 我们从不确定的类 的集合list 中,取出 里面的类。

反射一手,拿出里面的属性名, 做第一行表格行标题名称拼接。

②拼接内容

因为类不确定,那么我们就采取反射把类全部字段属性作为key丢到map里面,同时把值丢到value里面。

这样我们拼接内容的时候只需要根据map 嘎嘎一顿遍历 拼接即可。

1.依赖


    org.apache.poi
    poi-ooxml
    3.15


    org.apache.poi
    poi-scratchpad
    3.15


    com.alibaba
    fastjson
    1.2.69


    commons-io
    commons-io
    2.5

2. 核心的工具类,函数我都封装好了

MyCsvFileUtil.java

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author JCccc
 * @Remark 是我
 */
@Slf4j
public class MyCsvFileUtil {
    public static final String FILE_SUFFIX = ".csv";
    public static final String CSV_DELIMITER = ",";
    public static final String CSV_TAIL = "\r\n";
    protected static final String DATE_STR_FILE_NAME = "yyyyMMddHHmmssSSS";
 
    /**
     * 将字符串转成csv文件
     */
    public static void createCsvFile(String savePath, String contextStr) throws IOException {
 
        File file = new File(savePath);
        //创建文件
        file.createNewFile();
        //创建文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //将指定字节写入此文件输出流
        fileOutputStream.write(contextStr.getBytes("gbk"));
        fileOutputStream.flush();
        fileOutputStream.close();
    }
 
    /**
     * 写文件
     *
     * @param fileName
     * @param content
     */
    public static void writeFile(String fileName, String content) {
        FileOutputStream fos = null;
        OutputStreamWriter writer = null;
        try {
            fos = new FileOutputStream(fileName, true);
            writer = new OutputStreamWriter(fos, "GBK");
            writer.write(content);
            writer.flush();
        } catch (Exception e) {
            log.error("写文件异常|{}", e);
        } finally {
            if (fos != null) {
                IOUtils.closeQuietly(fos);
            }
            if (writer != null) {
                IOUtils.closeQuietly(writer);
            }
        }
    }
 
    /**
     * 构建文件名称
     * @param dataList
     * @return
     */
    public static String buildCsvFileFileName(List dataList) {
        return dataList.get(0).getClass().getSimpleName() + new SimpleDateFormat(DATE_STR_FILE_NAME).format(new Date()) + FILE_SUFFIX;
    }
 
    /**
     * 构建excel 标题行名
     * @param dataList
     * @return
     */
    public static String buildCsvFileTableNames(List dataList) {
        Map map = toMap(dataList.get(0));
        StringBuilder tableNames = new StringBuilder();
        for (String key : map.keySet()) {
            tableNames.append(key).append(MyCsvFileUtil.CSV_DELIMITER);
        }
        return tableNames.append(MyCsvFileUtil.CSV_TAIL).toString();
    }
 
    /**
     * 构建excel内容
     * @param dataLists
     * @return
     */
    public static String buildCsvFileBodyMap(List dataLists) {
        //不管你传什么玩意,我都给你反射一手,搞成Map
        List> mapList = new ArrayList<>();
        for (Object o : dataLists) {
            mapList.add(toMap(o));
        }
        //然后利用csv格式,对着map嘎嘎一顿拼接数据
        StringBuilder lineBuilder = new StringBuilder();
        for (Map rowData : mapList) {
            for (String key : rowData.keySet()) {
                Object value = rowData.get(key);
                if (Objects.nonNull(value)) {
                    lineBuilder.append(value).append(MyCsvFileUtil.CSV_DELIMITER);
                } else {
                    lineBuilder.append("--").append(MyCsvFileUtil.CSV_DELIMITER);
                }
            }
            lineBuilder.append(MyCsvFileUtil.CSV_TAIL);
        }
        return lineBuilder.toString();
    }
 
    /**
     * 类转map
     * @param entity
     * @param 
     * @return
     */
    public static Map toMap(T entity){
        Class bean = entity.getClass();
        Field[] fields = bean.getDeclaredFields();
        Map map = new HashMap<>(fields.length);
        for(Field field:fields){
            try {
                if(!"serialVersionUID".equals(field.getName())){
                    String methodName = "get"+field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1);
                    Method method = bean.getDeclaredMethod(methodName);
                    Object fieldValue = method.invoke(entity);
                    map.put(field.getName(),fieldValue);
                }
            } catch (Exception e) {
                log.warn("toMap() Exception={}",e.getMessage());
            }
        }
        return map;
    }
}

代码注意点(各种小封装):

①类转map

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第4张图片

② 反射转map 取字段属性名 拼接 标题

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第5张图片

③ 针对list<不确定类> 转化成 list ,然后拼接excel内容

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第6张图片

测试代码:

@RequestMapping("/createCsvFileJcTest")
public void createCsvFileJcTest() {
    //类不确定 随便怎么传都行
    List districts = districtService.queryByParentCodes(Arrays.asList("110100"));
    //存放地址&文件名
    String fileName = "D:\\mycsv\\"+MyCsvFileUtil.buildCsvFileFileName(districts);
    //创建表格行标题
    String tableNames = MyCsvFileUtil.buildCsvFileTableNames(districts);
    //创建文件
    MyCsvFileUtil.writeFile(fileName, tableNames);
    //写入数据
    String contentBody = MyCsvFileUtil.buildCsvFileBodyMap(districts);
    //调用方法生成
    MyCsvFileUtil.writeFile(fileName, contentBody);
}
SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第7张图片

看看效果:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第8张图片 SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第9张图片

导出的excel文件内容:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第10张图片

接下来换个类玩玩:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第11张图片

然后导出看看效果:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第12张图片

可以看到数据导出也是OK的:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第13张图片

没错就是这么简单, 当然也是抛转引玉, 希望大家看了这篇文章,可以借鉴这些反射的函数玩法,做更多的好玩的封装,比如加上一些自定义注解的解析,比如加上一些前后置拦截器拓展等等。


扩展

上面的示例中导出的表头是属性名,如果正式的导出通常需要自定义表头名称,我们这里可以使用自定义注解来完成。

JcExcelName.java

/**
 * @Author : JCccc
 * @CreateTime : 2020/5/14
 * @Description :
 **/
 
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface JcExcelName {
 
    String name() default "";
 
}

然后在想导出的类里面,想加看得懂的名字就加,不加就拿属性名:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第14张图片

随手再写一个 ,新的反射解析拿字段属性注解值函数:

public static  List resolveExcelTableName(T entity) {
    List tableNamesList = new ArrayList<>();
    Class bean = entity.getClass();
    Field[] fields = bean.getDeclaredFields();
    Map map = new HashMap<>(fields.length);
    for (Field field : fields) {
        try {
            if (!"serialVersionUID".equals(field.getName())) {
                String tableTitleName = field.getName();
                JcExcelName myFieldAnn = field.getAnnotation(JcExcelName.class);
                String annName = myFieldAnn.name();
                if (StringUtils.hasLength(annName)) {
                    tableTitleName = annName;
                }
                tableNamesList.add(tableTitleName);
            }
        } catch (Exception e) {
            log.warn("toMap() Exception={}", e.getMessage());
        }
    }
    return tableNamesList;
}

然后根据解析出来的注解值列名拼接 表格标题名格式:

public static String buildCsvFileTableNamesNew(List dataList) {
    StringBuilder tableNames = new StringBuilder();
    for (String name : dataList) {
        tableNames.append(name).append(MyCsvFileUtil.CSV_DELIMITER);
    }
    return tableNames.append(MyCsvFileUtil.CSV_TAIL).toString();
}

测试看看效果:

public static void main(String[] args) {

    User user = new User();
    List nameList = MapUtils.resolveExcelTableName(user);
    System.out.println(nameList.toString());
    String tableNames = buildCsvFileTableNamesNew(nameList);
    System.out.println(tableNames);

}

效果嘎嘎好:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第15张图片

然后反手就搞到我们前面的文章使用例子里面:

String tableNames = MyCsvFileUtil.buildCsvFileTableNamesNew( MyCsvFileUtil.resolveExcelTableName(dataList.get(0)));
SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第16张图片

执行一下示例接口,看看效果:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第17张图片

文件出来了:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第18张图片

打开看看效果:

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第19张图片

好了,就到这吧,非常完美。

作者:小目标青年

来源:blog.csdn.net/qq_35387940/article/

details/129062470


精彩推荐

1.阿里二面:使用 try-catch 捕获异常会影响性能吗?大部分人都会答错!
2.直接拿去赚钱的 ChatGPT 开源项目...
3.写了个工具,CRUD 开发效率直接提升100倍!生产力拉满...
4.公司入职一个阿里大佬,把SpringBoot项目启动从420秒优化到了40秒!
5.撸了一个SpringBoot+Vue 实现网页版人脸登录、人脸识别,逼格很高!!!
6.Spring Security 保姆级教程!40000字!
7.阿里版ChatGPT已进入测试,中文聊天截图曝光!
8.HTTPS 终于搞懂了 !

SpringBoot 我随手封装了一个万能的导出excel工具,传什么都能导出_第20张图片

你可能感兴趣的:(spring,boot,excel,java,后端,spring)