CC2530软件模拟I2C驱动24CXX EEPROM:从时序到实战优化
2026/6/6 13:15:42
一 功能总览与关键点
二 后端实现要点与代码
@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);}}@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();}@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);}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();}}三 前端实现要点与代码
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}四 常见问题与优化建议
五 依赖与配置建议
<!-- 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>以上文档覆盖了从模板下载、批量导入到数据导出的完整链路,并给出了关键代码示例与优化方向。后续可结合 EasyExcel 或自研模板引擎,进一步提升可维护性与性能。