深圳做网站推广公司哪家好,搜索引擎大全网址,安装wordpress数据库错误,在建工程查询网站一 功能总览与关键点
模板下载#xff1a;从 classpath 读取固定模板文件#xff0c;通过 HttpServletResponse 输出为附件#xff0c;设置正确的 Content-Type 与 Content-Disposition#xff0c;兼容中文文件名。批量导入#xff1a;接收 MultipartFile#xff0c;校验…一 功能总览与关键点模板下载从classpath读取固定模板文件通过HttpServletResponse输出为附件设置正确的Content-Type与Content-Disposition兼容中文文件名。批量导入接收MultipartFile校验后缀使用Apache POI WorkbookFactory解析.xls/.xlsx按行读取并映射为领域对象落库返回成功条数与失败原因。数据导出按查询条件或ids查询数据转换为VO使用自研或第三方工具写出到HttpServletResponse支持大数据量分 Sheet 写入。关键关注点模板路径与资源加载建议使用ClassPathResource。导入时单元格取值的“空值/类型”安全处理。关联字典如应用领域需做“名称→ID”的容错查询。导出时中文文件名编码与多浏览器兼容建议RFC 2231方式。资源关闭与异常兜底避免连接/句柄泄漏。二 后端实现要点与代码模板下载 ControllerOperation(summary数据导入)GetMapping(/downloadApplicationStandardBatchTemplate)publicvoiddownloadApplicationStandardBatchTemplate(HttpServletResponseresponse){BufferedInputStreambisnull;BufferedOutputStreambosnull;try{response.reset();response.setContentType(application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charsetutf-8);StringfileNameapplication_standard_batch.xlsx;// 兼容中文文件名RFC 2231StringencodedFilenameURLEncoder.encode(fileName,StandardCharsets.UTF_8).replaceAll(\\,%20);response.setHeader(Content-Disposition,attachment; filename\encodedFilename\; filename*UTF-8encodedFilename);response.setHeader(Access-Control-Expose-Headers,Content-Disposition);ResourceresourcenewClassPathResource(/applicationStandard/fileName);try(InputStreaminresource.getInputStream();ServletOutputStreamoutresponse.getOutputStream()){bisnewBufferedInputStream(in);bosnewBufferedOutputStream(out);byte[]buffnewbyte[2048];intbytesRead;while((bytesReadbis.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){LoginUserloginUserSecurityFrameworkUtils.getLoginUser();if(loginUsernull){returnCommonResult.error(BAD_REQUEST.getCode(),非法操作);}if(filenull||file.isEmpty()){returnCommonResult.error(BAD_REQUEST.getCode(),批量导入的Excel文件不能为空);}StringfileNamefile.getOriginalFilename();if(!ImageUtils.checkExcel(fileName)){returnCommonResult.error(BAD_REQUEST.getCode(),上传Excel后缀不符合要求);}try(InputStreamisfile.getInputStream()){WorkbookworkbookWorkbookFactory.create(is);Sheetsheetworkbook.getSheetAt(0);introwCountsheet.getPhysicalNumberOfRows();if(rowCount1){returnCommonResult.error(BAD_REQUEST.getCode(),模板无数据);}intsuccessNum0;for(inti1;irowCount;i){// 第0行为表头Rowrowsheet.getRow(i);if(rownull)continue;SesApplicationStandardContententitynewSesApplicationStandardContent();Stringv0getCellString(row.getCell(0));if(StringUtils.hasText(v0))entity.setStandardName(v0);Stringv1getCellString(row.getCell(1));if(StringUtils.hasText(v1))entity.setStandardVersion(v1);Stringv2getCellString(row.getCell(2));if(StringUtils.hasText(v2))entity.setTestItem(v2);Stringv3getCellString(row.getCell(3));if(StringUtils.hasText(v3))entity.setTestPort(v3);Stringv4getCellString(row.getCell(4));if(StringUtils.hasText(v4))entity.setTestLevel(v4);StringenvNamegetCellString(row.getCell(5));if(StringUtils.hasText(envName)){SesApplicationEnvironmentenvnewSesApplicationEnvironment();env.setEnvironmentName(envName);ListSesApplicationEnvironmentlistsesApplicationEnvironmentService.findSelect(env);if(CollectionUtils.isNotEmpty(list)){entity.setSesApplicationEnvironmentId(list.get(0).getId());}else{// 可选记录“未匹配到应用领域”的错误信息便于导入回执}}Stringv6getCellString(row.getCell(6));if(StringUtils.hasText(v6))entity.setBaseStandard(v6);Stringc1getCellString(row.getCell(10));if(StringUtils.hasText(c1))entity.setProductClassOne(c1);Stringc2getCellString(row.getCell(11));if(StringUtils.hasText(c2))entity.setProductClassTwo(c2);Stringc3getCellString(row.getCell(12));if(StringUtils.hasText(c3))entity.setProductClassThree(c3);Stringv13getCellString(row.getCell(13));if(StringUtils.hasText(v13))entity.setTestPortFeature(v13);Stringv14getCellString(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(cellnull)returnnull;returnnewDataFormatter().formatCellValue(cell).trim();}数据导出 ControllerOperation(summaryEMC应用标准测试内容-导出-v1.0)GetMapping(/export)publicvoidexport(SesApplicationStandardContentcondition,RequestParam(requiredfalse)Stringids,HttpServletResponseresponse)throwsIOException{ListSesApplicationStandardContentlist;if(StringUtils.hasText(ids)){ListLongidListArrays.stream(Convert.toStrArray(,,ids)).filter(StringUtils::hasText).map(Long::valueOf).toList();listsesApplicationStandardContentService.selectSesApplicationStandardContentByIds(idList);}else{listsesApplicationStandardContentService.selectSesApplicationStandardContentList(condition);}ListSesApplicationStandardContentVOvoListlist.stream().map(src-{SesApplicationStandardContentVOvonewSesApplicationStandardContentVO();BeanUtils.copyProperties(src,vo);if(src.getSesApplicationEnvironmentId()!null){SesApplicationEnvironmentenvsesApplicationEnvironmentService.getById(src.getSesApplicationEnvironmentId());vo.setEnvironmentName(env!null?env.getEnvironmentName():null);}returnvo;}).toList();ExcelUtilSesApplicationStandardContentVOutilnewExcelUtil(SesApplicationStandardContentVO.class);util.exportExcelToResponse(voList,EMC应用标准测试内容数据,response);}导出到响应的通用工具方法支持大数据量分 SheetpublicTvoidexportExcelToResponse(ListTlist,StringsheetName,HttpServletResponseresponse)throwsIOException{if(CollectionUtils.isEmpty(list)){response.setStatus(HttpServletResponse.SC_NO_CONTENT);return;}// 初始化创建 Workbook、设置字段、分页/分 Sheet 参数sheetSize 自定义this.init(list,sheetName,Excel.Type.EXPORT);doublesheetNoMath.ceil((double)list.size()/sheetSize);for(inti0;isheetNo;i){createSheet(sheetNo,i);Rowheadersheet.createRow(0);intcol0;for(Object[]os:fields){Excelexcel(Excel)os[1];createCell(excel,header,col);}if(Excel.Type.EXPORT.equals(type)){fillExcelData(index,header);addStatisticsRow();}}// 文件名编码与响应头StringencodedFilenameURLEncoder.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-8encodedFilename);response.setHeader(Access-Control-Expose-Headers,Content-Disposition);try(ServletOutputStreamoutresponse.getOutputStream()){wb.write(out);}finally{if(wb!null)wb.close();}}三 前端实现要点与代码导出按钮支持按选中 ID 或全量条件handleExport(){constqueryParamsaddSESDateRange(this.queryParams,this.dateRange,this.updateDateRange)if(this.idsthis.ids.length0){queryParams.idsthis.ids.join(,)}else{// 导出全部时移除分页参数Object.keys(queryParams).forEach(key{if([pageNum,pageSize].includes(key))deletequeryParams[key]})}constmsgthis.ids?.length?确认导出选中的${this.ids.length}条数据项?:确认导出所有符合条件的数据项?ElMessageBox.confirm(msg,警告,{type:warning}).then(()exportSesApplicationStandardContent(queryParams)).then(res{if(resres.size0){download.excel(res,应用标准测试内容.xlsx)ElMessage.success(导出成功)}else{ElMessage.error(导出失败返回数据为空)}}).catch(err{console.error(err)ElMessage.error(导出失败请检查网络或联系管理员)})}批量导入弹窗示例handleUpload(){this.uploadDialog.visibletrue}四 常见问题与优化建议模板下载中文文件名乱码使用URLEncoder.encode(…)filenameUTF-8’* 的RFC 2231方式兼容主流浏览器避免使用ISO-8859-1转码。导入时单元格取值异常使用DataFormatter统一将单元格格式化为字符串避免数字/日期类型导致的取值问题对null单元格做兜底。导入性能与内存大数据量时建议采用SAX/事件模式或EasyExcel进行流式读取分批入库避免OOM。关联字典容错对“应用领域”等字典字段名称→ID 查询无结果时记录错误明细支持导入回执与失败重试。导出大数据量采用分Sheet写入、分页查询、流式输出避免一次性将全部数据装入内存。安全性校验文件类型/大小、限制上传并发、校验登录态与权限对导入模板做版本管理避免结构变化导致解析失败。可观测性完善导入/导出日志与失败明细接入告警提供导入结果统计成功/失败/原因下载。五 依赖与配置建议核心依赖示例!-- Apache POI --dependencygroupIdorg.apache.poi/groupIdartifactIdpoi/artifactIdversion5.2.5/version/dependencydependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion5.2.5/version/dependency!-- 可选EasyExcel大数据量导入导出更省内存 --dependencygroupIdcom.alibaba/groupIdartifactIdeasyexcel/artifactIdversion3.3.3/version/dependency配置建议上传大小限制spring.servlet.multipart.max-file-size / max-request-size静态资源与模板将模板放入src/main/resources/applicationStandard/确保打包后位于classpath统一异常处理使用ControllerAdvice捕获 Excel 解析/IO 异常返回标准错误码与提示以上文档覆盖了从模板下载、批量导入到数据导出的完整链路并给出了关键代码示例与优化方向。后续可结合 EasyExcel 或自研模板引擎进一步提升可维护性与性能。