news 2026/4/16 15:33:08

含源码可复用: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/14 16:40:11

探索闭环 Cuk 转换器:从理论到仿真实践

闭环cuk转换器 [1]此仿真对于了解闭环 Cuk 转换器非常有用。 [2]您可以使用方程式轻松找到L&C的值。 方程式显示在 Simulink 文件中。最近在研究电源管理相关的内容&#xff0c;接触到了闭环 Cuk 转换器&#xff0c;发现它相当有趣&#xff0c;今天就来和大家分享一下我的学…

作者头像 李华
网站建设 2026/4/16 11:09:42

电子支付设备制造商,以Facebook+TikTok整合营销精准触达海外系统集成商和分销商

1、客户公司介绍本次合作客户是一家位于深圳的电子支付设备制造商&#xff0c;专注于智能卡读写器、移动POS终端等安全支付技术的研发与生产。作为一家拥有完善认证体系与强大研发团队的ODM/OEM企业&#xff0c;该公司致力于为全球合作伙伴提供定制化、经济高效的硬件解决方案与…

作者头像 李华
网站建设 2026/4/16 10:43:48

postman和Jmeter做接口测试的区别(经验之谈)

接口测试的目的 API 测试作为集成测试的一部分&#xff0c;经过被测应用的接口&#xff08;API&#xff09;来确定是否在功能、可靠性、性能和安全方面达到预期的软件测试。因为 API 都没有 GUI 界面&#xff0c;API 测试都是在通信层进行的。 1.建立接口用例集 Postman功能…

作者头像 李华
网站建设 2026/4/16 11:14:44

三菱PLC与组态王打造饮料自动装箱机控制系统

No.986 三菱PLC和组态王组态饮料自动装箱机控制系统设计在工业自动化领域&#xff0c;饮料自动装箱机的高效运行对于提高生产效率至关重要。今天咱们就来聊聊如何用三菱PLC和组态王来设计这样一个控制系统。 三菱PLC&#xff1a;核心控制大脑 三菱PLC作为控制核心&#xff0c;负…

作者头像 李华
网站建设 2026/4/14 23:05:12

APP UI自动化测试思路总结

首先想要说明一下&#xff0c;APP自动化测试可能很多公司不用&#xff0c;但也是大部分自动化测试工程师、高级测试工程师岗位招聘信息上要求的&#xff0c;所以为了更好的待遇&#xff0c;我们还是需要花时间去掌握的&#xff0c;毕竟谁也不会跟钱过不去。接下来&#xff0c;一…

作者头像 李华