news 2026/5/1 10:44:18

含源码可复用:Spring Boot 实现 Excel 导入导出与模板下载一站式方案

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
含源码可复用:Spring Boot 实现 Excel 导入导出与模板下载一站式方案

一 功能总览与关键点

  • 模板下载:从classpath读取固定模板文件,通过HttpServletResponse输出为附件,设置正确的Content-TypeContent-Disposition,兼容中文文件名。
  • 批量导入:接收MultipartFile,校验后缀,使用Apache POI WorkbookFactory解析.xls/.xlsx,按行读取并映射为领域对象,落库,返回成功条数与失败原因。
  • 数据导出:按查询条件或ids查询数据,转换为VO,使用自研或第三方工具写出到HttpServletResponse,支持大数据量分 Sheet 写入。
  • 关键关注点:
    • 模板路径与资源加载(建议使用ClassPathResource)。
    • 导入时单元格取值的“空值/类型”安全处理。
    • 关联字典(如应用领域)需做“名称→ID”的容错查询。
    • 导出时中文文件名编码与多浏览器兼容(建议RFC 2231方式)。
    • 资源关闭与异常兜底,避免连接/句柄泄漏。

二 后端实现要点与代码

  • 模板下载 Controller
@Operation(summary="数据导入")@GetMapping("/downloadApplicationStandardBatchTemplate")publicvoiddownloadApplicationStandardBatchTemplate(HttpServletResponseresponse){BufferedInputStreambis=null;BufferedOutputStreambos=null;try{response.reset();response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");StringfileName="application_standard_batch.xlsx";// 兼容中文文件名:RFC 2231StringencodedFilename=URLEncoder.encode(fileName,StandardCharsets.UTF_8).replaceAll("\\+","%20");response.setHeader("Content-Disposition","attachment; filename=\""+encodedFilename+"\"; filename*=UTF-8''"+encodedFilename);response.setHeader("Access-Control-Expose-Headers","Content-Disposition");Resourceresource=newClassPathResource("/applicationStandard/"+fileName);try(InputStreamin=resource.getInputStream();ServletOutputStreamout=response.getOutputStream()){bis=newBufferedInputStream(in);bos=newBufferedOutputStream(out);byte[]buff=newbyte[2048];intbytesRead;while((bytesRead=bis.read(buff))!=-1){bos.write(buff,0,bytesRead);}bos.flush();}}catch(Exceptione){// 建议统一异常处理(全局异常处理器),便于监控与告警log.error("下载模板失败",e);response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);}finally{// try-with-resources 已关闭流,这里兜底IOUtils.closeQuietly(bis);IOUtils.closeQuietly(bos);}}
  • 批量导入 Controller(含字典映射与校验)
@Operation(summary="标准数据模板批量导入-v1.0")@PostMapping("/import")publicCommonResultimportCase(@RequestParam("uploadFile")MultipartFilefile){LoginUserloginUser=SecurityFrameworkUtils.getLoginUser();if(loginUser==null){returnCommonResult.error(BAD_REQUEST.getCode(),"非法操作");}if(file==null||file.isEmpty()){returnCommonResult.error(BAD_REQUEST.getCode(),"批量导入的Excel文件不能为空");}StringfileName=file.getOriginalFilename();if(!ImageUtils.checkExcel(fileName)){returnCommonResult.error(BAD_REQUEST.getCode(),"上传Excel后缀不符合要求");}try(InputStreamis=file.getInputStream()){Workbookworkbook=WorkbookFactory.create(is);Sheetsheet=workbook.getSheetAt(0);introwCount=sheet.getPhysicalNumberOfRows();if(rowCount<=1){returnCommonResult.error(BAD_REQUEST.getCode(),"模板无数据");}intsuccessNum=0;for(inti=1;i<rowCount;i++){// 第0行为表头Rowrow=sheet.getRow(i);if(row==null)continue;SesApplicationStandardContententity=newSesApplicationStandardContent();Stringv0=getCellString(row.getCell(0));if(StringUtils.hasText(v0))entity.setStandardName(v0);Stringv1=getCellString(row.getCell(1));if(StringUtils.hasText(v1))entity.setStandardVersion(v1);Stringv2=getCellString(row.getCell(2));if(StringUtils.hasText(v2))entity.setTestItem(v2);Stringv3=getCellString(row.getCell(3));if(StringUtils.hasText(v3))entity.setTestPort(v3);Stringv4=getCellString(row.getCell(4));if(StringUtils.hasText(v4))entity.setTestLevel(v4);StringenvName=getCellString(row.getCell(5));if(StringUtils.hasText(envName)){SesApplicationEnvironmentenv=newSesApplicationEnvironment();env.setEnvironmentName(envName);List<SesApplicationEnvironment>list=sesApplicationEnvironmentService.findSelect(env);if(CollectionUtils.isNotEmpty(list)){entity.setSesApplicationEnvironmentId(list.get(0).getId());}else{// 可选:记录“未匹配到应用领域”的错误信息,便于导入回执}}Stringv6=getCellString(row.getCell(6));if(StringUtils.hasText(v6))entity.setBaseStandard(v6);Stringc1=getCellString(row.getCell(10));if(StringUtils.hasText(c1))entity.setProductClassOne(c1);Stringc2=getCellString(row.getCell(11));if(StringUtils.hasText(c2))entity.setProductClassTwo(c2);Stringc3=getCellString(row.getCell(12));if(StringUtils.hasText(c3))entity.setProductClassThree(c3);Stringv13=getCellString(row.getCell(13));if(StringUtils.hasText(v13))entity.setTestPortFeature(v13);Stringv14=getCellString(row.getCell(14));if(StringUtils.hasText(v14))entity.setMaintenanceResponsiblePerson(v14);sesApplicationStandardContentService.insertSesApplicationStandardContent(entity);successNum++;}returnCommonResult.success("导入成功,共 "+successNum+" 条");}catch(EncryptedDocumentExceptione){log.error("导入Excel文件加密或格式异常",e);returnCommonResult.error(BAD_REQUEST.getCode(),"Excel文件无法解析(可能加密)");}catch(IOExceptione){log.error("导入Excel文件IO异常",e);returnCommonResult.error(BAD_REQUEST.getCode(),"Excel文件读取失败");}}// 安全读取单元格为字符串(容错空/数字/日期等)privateStringgetCellString(Cellcell){if(cell==null)returnnull;returnnewDataFormatter().formatCellValue(cell).trim();}
  • 数据导出 Controller
@Operation(summary="EMC应用标准测试内容-导出-v1.0")@GetMapping("/export")publicvoidexport(SesApplicationStandardContentcondition,@RequestParam(required=false)Stringids,HttpServletResponseresponse)throwsIOException{List<SesApplicationStandardContent>list;if(StringUtils.hasText(ids)){List<Long>idList=Arrays.stream(Convert.toStrArray(",",ids)).filter(StringUtils::hasText).map(Long::valueOf).toList();list=sesApplicationStandardContentService.selectSesApplicationStandardContentByIds(idList);}else{list=sesApplicationStandardContentService.selectSesApplicationStandardContentList(condition);}List<SesApplicationStandardContentVO>voList=list.stream().map(src->{SesApplicationStandardContentVOvo=newSesApplicationStandardContentVO();BeanUtils.copyProperties(src,vo);if(src.getSesApplicationEnvironmentId()!=null){SesApplicationEnvironmentenv=sesApplicationEnvironmentService.getById(src.getSesApplicationEnvironmentId());vo.setEnvironmentName(env!=null?env.getEnvironmentName():null);}returnvo;}).toList();ExcelUtil<SesApplicationStandardContentVO>util=newExcelUtil<>(SesApplicationStandardContentVO.class);util.exportExcelToResponse(voList,"EMC应用标准测试内容数据",response);}
  • 导出到响应的通用工具方法(支持大数据量分 Sheet)
public<T>voidexportExcelToResponse(List<T>list,StringsheetName,HttpServletResponseresponse)throwsIOException{if(CollectionUtils.isEmpty(list)){response.setStatus(HttpServletResponse.SC_NO_CONTENT);return;}// 初始化:创建 Workbook、设置字段、分页/分 Sheet 参数(sheetSize 自定义)this.init(list,sheetName,Excel.Type.EXPORT);doublesheetNo=Math.ceil((double)list.size()/sheetSize);for(inti=0;i<=sheetNo;i++){createSheet(sheetNo,i);Rowheader=sheet.createRow(0);intcol=0;for(Object[]os:fields){Excelexcel=(Excel)os[1];createCell(excel,header,col++);}if(Excel.Type.EXPORT.equals(type)){fillExcelData(index,header);addStatisticsRow();}}// 文件名编码与响应头StringencodedFilename=URLEncoder.encode(sheetName,StandardCharsets.UTF_8).replaceAll("\\+","%20")+".xlsx";response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setHeader("Content-Disposition","attachment; filename=\""+encodedFilename+"\"; filename*=UTF-8''"+encodedFilename);response.setHeader("Access-Control-Expose-Headers","Content-Disposition");try(ServletOutputStreamout=response.getOutputStream()){wb.write(out);}finally{if(wb!=null)wb.close();}}

三 前端实现要点与代码

  • 导出按钮(支持按选中 ID 或全量条件)
handleExport(){constqueryParams=addSESDateRange(this.queryParams,this.dateRange,this.updateDateRange)if(this.ids&&this.ids.length>0){queryParams.ids=this.ids.join(',')}else{// 导出全部时移除分页参数Object.keys(queryParams).forEach(key=>{if(['pageNum','pageSize'].includes(key))deletequeryParams[key]})}constmsg=this.ids?.length?`确认导出选中的${this.ids.length}条数据项?`:'确认导出所有符合条件的数据项?'ElMessageBox.confirm(msg,'警告',{type:'warning'}).then(()=>exportSesApplicationStandardContent(queryParams)).then(res=>{if(res&&res.size>0){download.excel(res,'应用标准测试内容.xlsx')ElMessage.success('导出成功')}else{ElMessage.error('导出失败,返回数据为空')}}).catch(err=>{console.error(err)ElMessage.error('导出失败,请检查网络或联系管理员')})}
  • 批量导入弹窗(示例)
handleUpload(){this.uploadDialog.visible=true}

四 常见问题与优化建议

  • 模板下载中文文件名乱码
    • 使用URLEncoder.encode(…)+filename=UTF-8’'* 的RFC 2231方式,兼容主流浏览器;避免使用ISO-8859-1转码。
  • 导入时单元格取值异常
    • 使用DataFormatter统一将单元格格式化为字符串,避免数字/日期类型导致的取值问题;对null单元格做兜底。
  • 导入性能与内存
    • 大数据量时,建议采用SAX/事件模式EasyExcel进行流式读取,分批入库,避免OOM
  • 关联字典容错
    • 对“应用领域”等字典字段,名称→ID 查询无结果时记录错误明细,支持导入回执与失败重试。
  • 导出大数据量
    • 采用分Sheet写入、分页查询、流式输出,避免一次性将全部数据装入内存。
  • 安全性
    • 校验文件类型/大小、限制上传并发、校验登录态与权限;对导入模板做版本管理,避免结构变化导致解析失败。
  • 可观测性
    • 完善导入/导出日志与失败明细,接入告警;提供导入结果统计(成功/失败/原因)下载。

五 依赖与配置建议

  • 核心依赖(示例)
<!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.5</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.5</version></dependency><!-- 可选:EasyExcel(大数据量导入导出更省内存) --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.3</version></dependency>
  • 配置建议
    • 上传大小限制:spring.servlet.multipart.max-file-size / max-request-size
    • 静态资源与模板:将模板放入src/main/resources/applicationStandard/,确保打包后位于classpath
    • 统一异常处理:使用@ControllerAdvice捕获 Excel 解析/IO 异常,返回标准错误码与提示

以上文档覆盖了从模板下载、批量导入到数据导出的完整链路,并给出了关键代码示例与优化方向。后续可结合 EasyExcel 或自研模板引擎,进一步提升可维护性与性能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/29 7:59:06

大数据量 Excel 导入的性能与内存优化实战

大数据量 Excel 导入的性能与内存优化实战 一 核心原则 使用流式/事件驱动读取&#xff08;如 EasyExcel、POI SAX&#xff09;&#xff0c;避免 XSSFWorkbook 一次性将整表加载进内存&#xff0c;内存占用可做到与文件大小基本无关。采用分批处理 批量写入&#xff0c;每批积…

作者头像 李华
网站建设 2026/5/1 4:54:43

AI大模型通用技术架构图,非常详细收藏我这一篇就够了

AI大模型通用技术架构图​最后 我在一线科技企业深耕十二载&#xff0c;见证过太多因技术卡位而跃迁的案例。那些率先拥抱 AI 的同事&#xff0c;早已在效率与薪资上形成代际优势&#xff0c;我意识到有很多经验和知识值得分享给大家&#xff0c;也可以通过我们的能力和经验解答…

作者头像 李华
网站建设 2026/5/1 6:11:48

小白入门大模型产品经理:完整学习路线与免费资源大礼包_LLM大模型产品经理学习路线

文章提供了大模型产品经理的完整学习路线&#xff0c;包括基础知识、大模型技术、产品管理、实战经验和持续提升五个阶段。同时介绍了七阶段AI大模型学习计划&#xff0c;涵盖系统设计、提示词工程、平台应用开发等内容。文章还提供了一系列免费学习资源&#xff0c;包括学习路…

作者头像 李华
网站建设 2026/5/1 7:34:44

SpringCloud —— Sentinel详解

一、前言在前面的课程中&#xff0c;我们可以发现一个问题&#xff0c;就是即使是低耦合的微服务架构&#xff0c;在微服务和微服务之间依然是有耦合的&#xff0c;比如相互之间的远程调用&#xff0c;这就导致一个微服务出现异常&#xff08;不一定是崩溃&#xff0c;也有可能…

作者头像 李华
网站建设 2026/5/1 7:19:27

解决鸿蒙PC命令行编译 macOS 上 cp 命令参数冲突问题

解决鸿蒙PC命令行编译 macOS 上 cp 命令参数冲突问题 问题背景 在 macOS 系统上编译 OpenHarmony PC 命令行 项目时&#xff0c;运行构建脚本 build.sh 遇到了以下错误&#xff1a; cp: the -R and -r options may not be specified together这个错误导致后续的构建流程无法…

作者头像 李华