万网网站建设步骤,广告设计培训哪家好,企业内部网站如何建设,手机app网站开发前言 如题#xff0c;这个小玩意#xff0c;就是不限制你查的是哪张表#xff0c;用的是什么类。 
我直接一把梭#xff0c;嘎嘎给你一顿导出。 我知道#xff0c;这是很多人都想过的#xff0c; 至少我就收到很多人问过我这个类似的问题。 
我也跟他们说了#xff0c;但…前言 如题这个小玩意就是不限制你查的是哪张表用的是什么类。 
我直接一把梭嘎嘎给你一顿导出。 我知道这是很多人都想过的 至少我就收到很多人问过我这个类似的问题。 
我也跟他们说了但是他们就是不动手其实真的很简单。 
不动手怎么办  我出手呗。   
不多说开搞 。 正文 
玩法很简单我之前有写过一篇利用csv文件内容格式做excel文件导出的。 
如果没有看过的还等什么现在就去看看 
Springboot 那年我双手插兜手写一个excel导出 要实现的效果  类是不确定的 User  Student ?  District ?   不确定。  但是呢我们封装出来的函数要足够支撑不同的类我们自动去读取遍历list 然后导出生成文件。   核心的思路是什么     
其实就还是利用csv文件的内容格式本质 看这两幅图  我们要实现万能的类导出excel  思路是什么  ① 我们从不确定的类 的集合list 中取出 里面的类。 反射一手拿出里面的属性名 做第一行表格行标题名称拼接。  ②拼接内容 因为类不确定那么我们就采取反射把类全部字段属性作为key丢到map里面 同时把值丢到value里面。 这样我们拼接内容的时候只需要根据map 嘎嘎一顿遍历 拼接即可。 1.依赖 dependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion3.15/version/dependencydependencygroupIdorg.apache.poi/groupIdartifactIdpoi-scratchpad/artifactIdversion3.15/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.69/version/dependencydependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.5/version/dependency 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) {MapString, Object 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) {//不管你传什么玩意我都给你反射一手搞成MapListMapString, Object mapList  new ArrayList();for (Object o : dataLists) {mapList.add(toMap(o));}//然后利用csv格式对着map嘎嘎一顿拼接数据StringBuilder lineBuilder  new StringBuilder();for (MapString, Object 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 T* return*/public staticT MapString, Object toMap(T entity){Class? extends Object bean  entity.getClass();Field[] fields  bean.getDeclaredFields();MapString, Object map  new HashMap(fields.length);for(Field field:fields){try {if(!serialVersionUID.equals(field.getName())){String methodName  getfield.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  ② 反射转map 取字段属性名 拼接 标题 ③ 针对list不确定类 转化成 listmap 然后拼接excel内容  测试代码   RequestMapping(/createCsvFileJcTest)public void createCsvFileJcTest() {//类不确定 随便怎么传都行ListDistrict 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);} 看看效果   导出的excel文件内容  接下来换个类玩玩 然后导出看看效果 
可以看到数据导出也是OK的  没错就是这么简单 当然也是抛转引玉 希望大家看了这篇文章可以借鉴这些反射的函数玩法做更多的好玩的封装比如加上一些自定义注解的解析比如加上一些前后置拦截器拓展等等。 好了该篇就到这。 ps 
Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入 
Springboot 指定自定义模板导出Excel文件 SpringBoot 导出多个Excel文件压缩成.zip格式下载 Springboot 获取导入的Excel文件的sheet表 列名  Springboot 导入导出Excel 一对多关系复合表格、合并单元格数据 Springboot 那年我双手插兜手写一个excel导出   文章转载自: http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.skwwj.cn.gov.cn.skwwj.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.jbqwb.cn.gov.cn.jbqwb.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.gcftl.cn.gov.cn.gcftl.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.qwzpd.cn.gov.cn.qwzpd.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.nhdmh.cn.gov.cn.nhdmh.cn http://www.morning.hblkq.cn.gov.cn.hblkq.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.pqchr.cn.gov.cn.pqchr.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.wzyfk.cn.gov.cn.wzyfk.cn http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn http://www.morning.lxngn.cn.gov.cn.lxngn.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.pwhjr.cn.gov.cn.pwhjr.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.jwrcz.cn.gov.cn.jwrcz.cn