news 2026/9/29 7:50:58

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

作者头像

张小明

前端开发工程师

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

一 功能总览与关键点

  • 模板下载:从classpath读取固定模板文件,通过HttpServletResponse输出为附件,设置正确的Content-Type与Content-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/9/29 5:52:09

2026校招AI人才需求报告:大模型工程师月薪近2.5万,这3类人才最抢手!

2026届校招市场AI人才需求呈现三大特征&#xff1a;需求稳增、结构优化、薪酬分化。高科技企业成为AI人才需求主力军&#xff0c;超60%企业已将AI人才纳入核心招聘目标。技术研发类岗位需求旺盛&#xff0c;大模型算法工程师月薪中位数达24760元。企业招聘更看重数学与算法基础…

作者头像 李华
网站建设 2026/9/29 3:39:18

做自动化测试最容易犯的错误,一篇带你少走弯路...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、自动化一切 我…

作者头像 李华
网站建设 2026/9/28 9:07:13

文献理论期末测试复习指南

你是不是也这样&#xff1a;下载的PDF堆满文件夹&#xff0c;想找的时候死活记不住名字&#xff1b;读文献时灵感一闪&#xff0c;回头却找不到记在哪了&#xff1b;写论文时&#xff0c;调整一个引用格式就要折腾半小时…文献管理不是小事&#xff0c;它直接决定了你的研究效率…

作者头像 李华
网站建设 2026/9/28 3:27:51

小白转行AI攻略:从产品经理到算法工程师,5大岗位技能与薪资全解析_普通人如何杀入AI赛道?(附岗位薪资与避坑指南)

文章详细解析了AI行业5大核心岗位&#xff08;产品经理、解决方案专家、应用工程师、算法工程师、AI运营&#xff09;的职责、薪资范围及所需技能。指出当前是入局AI的最佳窗口期&#xff0c;转行需掌握AI原理理解、数据准备、Prompt工程、RAG等关键技术。文章分析了目标人群画…

作者头像 李华
网站建设 2026/9/28 4:04:49

随时随地更文!VanBlog + cpolar让博客管理超自由

文章目录前言1. Linux本地部署2. VanBlog简单使用3. 安装内网穿透4. 创建公网地址5. 创建固定公网地址VanBlog 与内网穿透的结合&#xff0c;让博客创作和管理突破了地域限制&#xff0c;为创作者带来了更自由的体验。前言 VanBlog 是一款专注于简化建站流程的博客系统&#x…

作者头像 李华