三、动态导出导入工具类封装在实际使用开发中,我们不可能每来一个 excel 导入导出需求,就编写一个方法,而且很多业务需求都是动态导入导出,没办法基于实体类注解的方式来读取文件或者写入文件
因此,基于动态参数化生成文件和动态监听器读取文件方法,我们可以单独封装一套动态导出导出工具类,省的我们每次都需要重新编写大量重复工作,以下就是小编我在实际使用过程,封装出来的工具类,在此分享给大家!
- 动态导出工具类
public class DynamicEasyExcelExportUtils {private static final Logger log = LoggerFactory.getLogger(DynamicEasyExcelExportUtils.class);private static final String DEFAULT_SHEET_NAME = "sheet1";/*** 动态生成导出模版(单表头)* @param headColumns 列名称* @returnexcel文件流*/public static byte[] exportTemplateExcelFile(List<String> headColumns){List<List<String>> excelHead = Lists.newArrayList();headColumns.forEach(columnName -> { excelHead.add(Lists.newArrayList(columnName)); });byte[] stream = createExcelFile(excelHead, new ArrayList<>());return stream;}/*** 动态生成模版(复杂表头)* @param excelHead列名称* @return*/public static byte[] exportTemplateExcelFileCustomHead(List<List<String>> excelHead){byte[] stream = createExcelFile(excelHead, new ArrayList<>());return stream;}/*** 动态导出文件* @param headColumnMap有序列头部* @param dataList数据体* @return*/public static byte[] exportExcelFile(LinkedHashMap<String, String> headColumnMap, List<Map<String, Object>> dataList){//获取列名称List<List<String>> excelHead = new ArrayList<>();if(MapUtils.isNotEmpty(headColumnMap)){//key为匹配符,value为列名,如果多级列名用逗号隔开headColumnMap.entrySet().forEach(entry -> {excelHead.add(Lists.newArrayList(entry.getValue().split(",")));});}List<List<Object>> excelRows = new ArrayList<>();if(MapUtils.isNotEmpty(headColumnMap) && CollectionUtils.isNotEmpty(dataList)){for (Map<String, Object> dataMap : dataList) {List<Object> rows = new ArrayList<>();headColumnMap.entrySet().forEach(headColumnEntry -> {if(dataMap.containsKey(headColumnEntry.getKey())){Object data = https://www.jinnalai.com/fenxiang/dataMap.get(headColumnEntry.getKey());rows.add(data);}});excelRows.add(rows);}}byte[] stream = createExcelFile(excelHead, excelRows);return stream;}/*** 生成文件* @param excelHead* @param excelRows* @return*/private static byte[] createExcelFile(List> excelHead, List> excelRows){try {if(CollectionUtils.isNotEmpty(excelHead)){ByteArrayOutputStream outputStream = new ByteArrayOutputStream();EasyExcel.write(outputStream).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).head(excelHead).sheet(DEFAULT_SHEET_NAME).doWrite(excelRows);return outputStream.toByteArray();}} catch (Exception e) {log.error("动态生成excel文件失败,headColumns:" + JSONArray.toJSONString(excelHead) + ",excelRows:" + JSONArray.toJSONString(excelRows), e);}return null;}/*** 导出文件测试* @param args* @throws IOException*/public static void main(String[] args) throws IOException {//导出包含数据内容的文件LinkedHashMap<String, String> headColumnMap = Maps.newLinkedHashMap();headColumnMap.put("className","班级");headColumnMap.put("name","学生信息,姓名");headColumnMap.put("sex","学生信息,性别");List<Map<String, Object>> dataList = new ArrayList<>();for (int i = 0; i < 5; i++) {Map<String, Object> dataMap = Maps.newHashMap();dataMap.put("className", "一年级");dataMap.put("name", "张三" + i);dataMap.put("sex", "男");dataList.add(dataMap);}byte[] stream = exportExcelFile(headColumnMap, dataList);FileOutputStream outputStream = new FileOutputStream(new File("/Users/panzhi/Documents/easyexcel-export-user5.xlsx"));outputStream.write(stream);outputStream.close();}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- java的excel的读取和写入 java操作excel
- ececl表格常用的公式 新手入门excel表格制作
- 如何用excel做回归分析 回归分析步骤推荐
- excel办公软件 excel办公软件基础知识
- jsp调用java方法 java基础面试题及答案
- 在线excel转json excel一键转换json
- 表格软件excel如何使用 excel表格免费版
- excel解密方法 excel解密三步法
- 堆排序代码解析 堆排序的算法及代码实现
- socket传输文件的原理 socket实现文件传输
