位置: IT常识 - 正文

EasyExcel解析动态表头及导出(easyexcel解析csv)

编辑:rootadmin
EasyExcel解析动态表头及导出 前言

推荐整理分享EasyExcel解析动态表头及导出(easyexcel解析csv),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:excel 解析,easyexcel动态列设置值,easyexcel解析动态表头,easyexcel解析动态表头,easyexcel动态生成模板详解,easyexcel解析csv,easyexcel解析动态表头,easyexcel动态生成模板详解,内容如对您有帮助,希望把文章链接给更多的朋友!

excel文件导入及导出,是日常开发中经常遇到的需求。本次笔者以EasyExcel为例,针对在项目中遇到的动态表头解析及导出的场景,详细介绍具体的代码实现过程。

参考地址

https://github.com/alibaba/easyexcel

前端下载 const download = () => { axios({ method: 'GET', url: config.http.baseUrl + '/templateDownload', responseType: 'blob', }) .then(function (res) { const content = res.data const blob = new Blob([content], { type: "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }) const downloadElement = document.createElement("a"); const href = window.URL.createObjectURL(blob); downloadElement.href = href; downloadElement.download = decodeURI(res.headers['filename']); document.body.appendChild(downloadElement); downloadElement.click(); document.body.removeChild(downloadElement); // 下载完成移除元素 window.URL.revokeObjectURL(href); // 释放掉blob对象 }) }模板下载

excel文件导入功能,常常需要进行模板下载,在springboot项目中,程序是以jar包的形式运行的,所以有很多小伙伴常常

遇到在本地开发中能够实现下载功能,但部署到服务器的时候,找不到模板文件的问题。

@Overridepublic void templateDownload(HttpServletResponse response, HttpServletRequest request) { //获取要下载的模板名称 String fileName = "批量导入模板.xlsx"; //获取文件下载路径 String filePath = "/template/template.xlsx"; TemplateDownloadUtil.download(response, request, fileName, filePath);}import lombok.extern.slf4j.Slf4j;import org.springframework.core.io.ClassPathResource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.InputStream;import java.io.OutputStream;import java.net.URLEncoder;/** * 模板文件下载工具类 * @author * @date 2021/05/20 9:20 */@Slf4jpublic class TemplateDownloadUtil { public static void download(HttpServletResponse response, HttpServletRequest request,String fileName,String filePath){ try { response.setContentType("application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setHeader("filename", URLEncoder.encode(fileName, "UTF-8")); response.setHeader("Access-Control-Expose-Headers", "filename,Content-Disposition"); //获取文件的路径,此方式本地开发可以运行,服务器无法获取文件// String filePath = getClass().getResource("/template/template.xlsx").getPath();// FileInputStream input = new FileInputStream(filePath); //在服务器中能够读取到模板文件 ClassPathResource resource = new ClassPathResource(filePath); InputStream input = resource.getInputStream(); OutputStream out = response.getOutputStream(); byte[] b = new byte[2048]; int len; while ((len = input.read(b)) != -1) { out.write(b, 0, len); } //修正 Excel在“xxx.xlsx”中发现不可读取的内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"// response.setHeader("Content-Length", String.valueOf(input.getChannel().size())); input.close(); } catch (Exception e) { log.error("下载模板失败 :", e); } }}EasyExcel动态表头解析

EasyExcel简单的读文件,官网中已经有详细的说明,本文不再赘述,详细操作参见

EasyExcel解析动态表头及导出(easyexcel解析csv)

本文主要针对笔者遇到的复杂表头及动态表头进行讲解。

模板示例

解析import com.alibaba.excel.context.AnalysisContext;import com.alibaba.excel.event.AnalysisEventListener;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import lombok.Data;import lombok.extern.slf4j.Slf4j;import java.time.LocalDateTime;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.stream.Collectors;/** * 发薪单上传excel读取类 * * @author yupf * @description Listener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 */@Slf4j@Datapublic class BatchReadListener extends AnalysisEventListener<Map<Integer, String>> { /** * 每隔500条存储数据库,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 500; //Excel数据缓存结构 private List<Map<Integer, Map<Integer, String>>> list = new ArrayList<>(); //Excel表头(列名)数据缓存结构 private Map<Integer, String> headTitleMap = new HashMap<>(); /** * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。 */ private DbFileBatchService dbFileBatchService; private DbFileContentService dbFileContentService; private FileBatch fileBatch; private int total = 0; /** * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来 */ public BatchReadListener(DbFileBatchService dbFileBatchService, DbFileContentService dbFileContentService, FileBatch fileBatch) { this.dbFileBatchService = dbFileBatchService; this.dbFileContentService = dbFileContentService; this.fileBatch = fileBatch; } /** * 这个每一条数据解析都会来调用 * * @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()} * @param context */ @Override public void invoke(Map<Integer, String> data, AnalysisContext context) { log.info("解析到一条数据:{}", JSON.toJSONString(data)); total++; Map<Integer, Map<Integer, String>> map = new HashMap<>(); map.put(context.readRowHolder().getRowIndex(), data); list.add(map); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM if (list.size() >= BATCH_COUNT) { saveData(); // 存储完成清理 list list.clear(); } } /** * 所有数据解析完成了 都会来调用 * * @param context */ @Override public void doAfterAllAnalysed(AnalysisContext context) { // 这里也要保存数据,确保最后遗留的数据也存储到数据库 saveData(); log.info("所有数据解析完成!"); } /** * 解析表头数据 **/ @Override public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) { log.info("表头数据:{}", JSONObject.toJSONString(headMap)); headTitleMap = headMap; } /** * 加上存储数据库 */ private void saveData() { log.info("{}条数据,开始存储数据库!", list.size()); FileContent fileContent = null; List<FileContent> fileContentList = list.stream().flatMap( integerMap -> integerMap.entrySet().stream().map(entrySet -> { //entrySet.getKey()获取的是内容的RowIndex,实际的行数需要根据表头数进行处理 Integer rowIndex = entrySet.getKey(); Map<Integer, String> value = entrySet.getValue(); log.info(JSONObject.toJSONString(value)); fileContent = new FileContent(); fileContent.setBatchId(fileBatch.getId()); fileContent.setBatchNo(fileBatch.getBatchNo()); //固定字段入库 fileContent.setName(value.get(0) != null ? value.get(0).trim() : ""); fileContent.setCertNo(value.get(1) != null ? value.get(1).trim() : ""); fileContent.setRealAmount(value.get(2) != null ? value.get(2).trim() : ""); //所有动态表头数据转为JSON串入库 fileContent.setFieldsValue(JSONObject.toJSONString(value)); //取实际的内容rowIndex fileContent.setRowNum(rowIndex + 1); fileContent.setCreateTime(LocalDateTime.now()); return xcSalaryFileContent; } )).collect(Collectors.toList()); log.info(JSONObject.toJSONString(fileContentList)); dbFileContentService.saveBatch(fileContentList); log.info("存储数据库成功!"); }} BatchReadListener listener = new BatchReadListener(dbFileBatchService, dbFileContentService, fileBatch); try { //注:headRowNumber默认为1,现赋值为2,即从第三行开始读取内容 EasyExcel.read(fileInputStream, listener).headRowNumber(2).sheet().doRead(); } catch (Exception e) { log.info("EasyExcel解析文件失败,{}", e); throw new CustomException("文件解析失败,请重新上传"); } //获取表头信息进行处理 Map<Integer, String> headTitleMap = listener.getHeadTitleMap(); //获取动态表头信息 List<String> headList = headTitleMap.keySet().stream().map(key -> { String head = headTitleMap.get(key); log.info(head); return head == null ? "" : head.replace("*", ""); }).collect(Collectors.toList()); //可以对表头进行入库保存,方便后续导出

综上,动态表头即可完成解析。

EasyExcel动态表头导出导出示例

获取动态头 private List<List<String>> getFileHeadList( FileBatch fileBatch) { String head = fileBatch.getFileHead(); List<String> headList = Arrays.asList(head.split(",")); List<List<String>> fileHead = headList.stream().map(item -> concatHead(Lists.newArrayList(item))).collect(Collectors.toList()); fileHead.add(concatHead(Lists.newArrayList("备注"))); return fileHead; } /** * 填写须知 * @param headContent * @return */private List<String> concatHead(List<String> headContent) { String remake = "填写须知: \n" + "1.系统自动识别Excel表格,表头必须含有“企业账户号”、“企业账户名”、“实发金额”;\n" + "2.带 “*” 为必填字段,填写后才能上传成功;\n" + "3.若需上传其他表头,可自行在“实发金额”后添加表头,表头最多可添加20个,表头名称请控制在8个字以内;\n" + "4.填写的表头内容不可超过30个字;\n" + "5.实发金额支持填写到2位小数;\n" + "6.每次导入数据不超过5000条。\n" + "\n" + "注:请勿删除填写须知,删除后将导致文件上传失败\n" + "\n" + "表头示例:"; headContent.add(0, remake); return headContent; }获取数据 List<FileContent> fileContentList = dbFileContentService.list( Wrappers.<FileContent>lambdaQuery() .eq(FileContent::getBatchId, fileBatch.getId()) .orderByAsc(FileContent::getRowNum) ); List<List<Object>> contentList = fileContentList.stream().map(fileContent -> { List<Object> rowList = new ArrayList<>(); String fieldsValue = fileContent.getFieldsValue(); JSONObject contentObj = JSONObject.parseObject(fieldsValue); for (int columnIndex = 0 , length = headList.size(); columnIndex < length; columnIndex++) { Object content = contentObj.get(columnIndex); rowList.add(content == null ? "" : content); } rowList.add(fileContent.getCheckMessage()); return rowList; }).collect(Collectors.toList());单元格格式设置import com.alibaba.excel.metadata.data.DataFormatData;import com.alibaba.excel.metadata.data.WriteCellData;import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;import com.alibaba.excel.write.metadata.style.WriteCellStyle;import com.alibaba.excel.write.metadata.style.WriteFont;import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;import org.apache.poi.ss.usermodel.BorderStyle;import org.apache.poi.ss.usermodel.HorizontalAlignment;import org.apache.poi.ss.usermodel.IndexedColors;import java.util.List;/** * 设置表头和填充内容的样式 */public class CellStyleStrategy extends HorizontalCellStyleStrategy { private final WriteCellStyle headWriteCellStyle; private final WriteCellStyle contentWriteCellStyle; /** * 操作列 */ private final List<Integer> columnIndexes; public CellStyleStrategy(List<Integer> columnIndexes,WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) { this.columnIndexes = columnIndexes; this.headWriteCellStyle = headWriteCellStyle; this.contentWriteCellStyle = contentWriteCellStyle; } //设置头样式 @Override protected void setHeadCellStyle( CellWriteHandlerContext context) { // 获取字体实例 WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontName("宋体"); //表头不同处理 if (columnIndexes.get(0).equals(context.getRowIndex())) { headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT); headWriteFont.setFontHeightInPoints((short) 12); headWriteFont.setBold(false); headWriteFont.setFontName("宋体"); }else{ headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headWriteFont.setFontHeightInPoints((short) 11); headWriteFont.setBold(false); headWriteFont.setFontName("微软雅黑"); } headWriteCellStyle.setWriteFont(headWriteFont); DataFormatData dataFormatData = new DataFormatData(); dataFormatData.setIndex((short)49); headWriteCellStyle.setDataFormatData(dataFormatData); if (stopProcessing(context)) { return; } WriteCellData<?> cellData = context.getFirstCellData(); WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle()); } //设置填充数据样式 @Override protected void setContentCellStyle(CellWriteHandlerContext context) { WriteFont contentWriteFont = new WriteFont(); contentWriteFont.setFontName("宋体"); contentWriteFont.setFontHeightInPoints((short) 11); //设置数据填充后的实线边框 contentWriteCellStyle.setWriteFont(contentWriteFont); contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); DataFormatData dataFormatData = new DataFormatData(); dataFormatData.setIndex((short)49); contentWriteCellStyle.setDataFormatData(dataFormatData); contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); WriteCellData<?> cellData = context.getFirstCellData(); WriteCellStyle.merge(contentWriteCellStyle, cellData.getOrCreateStyle()); }}行高设置import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy;import org.apache.poi.ss.usermodel.Row;/** * 设置表头的自动调整行高策略 */public class CellRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy { @Override protected void setHeadColumnHeight(Row row, int relativeRowIndex) { //设置主标题行高为17.7 if(relativeRowIndex == 0){ //如果excel需要显示行高为15,那这里就要设置为15*20=300 row.setHeight((short) 3240); } } @Override protected void setContentColumnHeight(Row row, int relativeRowIndex) { }}列宽度自适应

如果是简单表头,可以使用EasyExcel中的LongestMatchColumnWidthStyleStrategy()来实现。

EasyExcel.write(fileName, LongestMatchColumnWidthData.class) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("模板").doWrite(dataLong());

如果是复杂表头,就需要自己来实现,代码如下:

import com.alibaba.excel.enums.CellDataTypeEnum;import com.alibaba.excel.metadata.Head;import com.alibaba.excel.metadata.data.CellData;import com.alibaba.excel.metadata.data.WriteCellData;import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;import lombok.extern.slf4j.Slf4j;import org.apache.commons.collections.CollectionUtils;import org.apache.poi.ss.usermodel.Cell;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author yupf * @description * @date 2022/9/7 18:48 */@Slf4jpublic class CellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy { private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>(); @Override protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo()); if (maxColumnWidthMap == null) { maxColumnWidthMap = new HashMap<>(); CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap); } if (isHead) { if(relativeRowIndex.intValue() == 1){ Integer length = cell.getStringCellValue().getBytes().length; Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex()); if (maxColumnWidth == null || length > maxColumnWidth) { maxColumnWidthMap.put(cell.getColumnIndex(), length); writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), length * 300); } } }else{ Integer columnWidth = this.dataLength(cellDataList, cell, isHead); if (columnWidth >= 0) { if (columnWidth > 255) { columnWidth = 255; } Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex()); if (maxColumnWidth == null || columnWidth > maxColumnWidth) { maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth); writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256); } } } } private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) { if (isHead) { return cell.getStringCellValue().getBytes().length; } else { CellData cellData = cellDataList.get(0); CellDataTypeEnum type = cellData.getType(); if (type == null) { return -1; } else { switch (type) { case STRING: return cellData.getStringValue().getBytes().length; case BOOLEAN: return cellData.getBooleanValue().toString().getBytes().length; case NUMBER: return cellData.getNumberValue().toString().getBytes().length; default: return -1; } } } }}写入文件EasyExcel.write(response.getOutputStream()) .head(head) .registerWriteHandler(new CellRowHeightStyleStrategy()) //设置行高的策略 .registerWriteHandler(new CellStyleStrategy(Arrays.asList(0,1),new WriteCellStyle(), new WriteCellStyle())) .registerWriteHandler(new CellWidthStyleStrategy()) .sheet(sheetName) .doWrite(list);总结

以上便是EasyExcel解析动态表头及导出的整个过程。

在使用过程中,笔者的感受是,上手难度很低,很适合新手去做简单的表格解析,当然,如果你的需求有复杂的格式,EasyExcel也提供了api,能够很好的满足需要。

本文链接地址:https://www.jiuchutong.com/zhishi/284034.html 转载请保留说明!

上一篇:显卡是4G的1050TI好还是3G的1060好(1050ti 4g显卡)

下一篇:windows7安装node14版本及以上(windows7安装nodejs14)

  • 附加税减免要做哪些科目
  • 外贸企业出口货物
  • 公司代扣个人所得税有完税凭证明
  • 所得税费用包括递延所得税吗
  • 员工产假个税系统改为非正常要更正申报吗?
  • 出口销售收入要交增值税吗
  • 金蝶k3生产领料单怎么导入
  • 申报表季初资产和季末资产
  • 当期计提加计抵减额如何确定
  • 设计服务需要交文化建设费吗
  • 企业不得将生产经营项目、场所
  • 公司借款给个人超过一年未还
  • 企业购买预付卡怎么做账
  • 代收代付如何进行账务处理?
  • 单位参加城镇职工基本养老保险基数怎么填
  • 代收国税地税收入
  • 计提养老保险会计分录怎么做
  • 购进的出口货物如何入账
  • 税控盘280减免申报
  • 多交的增值税可以抵所得税吗?
  • 专利年费应该入哪个科目
  • 法人股东分红如何申报所得税
  • 鸿蒙负一屏怎么设置
  • 结转人工费会计分录
  • 计提劳务派遣人员社保收到发票后没有付款的会计分录
  • 部分苹果iOS17功能延后推出
  • 电脑输入法无法输入中文怎么办
  • 共享文档无法访问如何解决
  • linux系统参数调优
  • dev beta 发布预览
  • 投资收益怎么算例题
  • 如何区分货币财富和收入
  • php iswriteable
  • 嘴型融合 wav2lip 升级版
  • 境外服务收入如何申报增值税
  • 广告费业务宣传费可以结转吗
  • 数据集介绍
  • 傅里叶变换的过程
  • metric命令
  • ci框架视频教程
  • php执行系统命令函数
  • 浅谈建筑地基基础加固施工技术亲
  • nslookup命令大全
  • 个税专项附加扣除具体怎么操作
  • 增值税进项税额计算公式
  • 坏账准备计提范围包括
  • 公司转让注册海外公司
  • java自增自减运算符的规则
  • 工会经费发放奖励个税处理
  • 返还个人所得税怎么操作
  • 最新个人捐款是什么意思
  • 捐赠货物的增值税可以税前扣除吗
  • 运输公司内账会计每天需要做什么
  • 外地施工企业异地施工
  • 营业费用占比
  • 职工薪酬包括的内容
  • 出口免抵退和免退有什么区别
  • 开销项负数的流程
  • 收付实现制与权责发生制的例题
  • 低值易耗品最新定义
  • c#如何使用数据库
  • win10预览版选哪个
  • tcpsyncookies
  • centos更新命令
  • win10添加闹钟
  • win10 自带
  • win7的库是什么
  • linux系统中scp命令的使用介绍
  • win10系统谷歌浏览器为什么打不开
  • 如何在linux中安装软件
  • opengl编程实例
  • python教程100例
  • shell脚本编程实例
  • Unity GameObject.activeSelf, GameObject.activeInHierarchy,GameObject.SetActive和SetActiveRecursively
  • 税务分局长级别
  • 国家税务总局上海市电子税务局
  • 财产保险合同的种类
  • 丹东银行总行电话
  • 税务局文化品牌
  • 入库税收怎么根据完税证明
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设