当前位置: 首页 > news >正文

POI导入时相关的EXCEL校验

一、单元格值非空校验

    public static Boolean poiCellIsNull(Cell cell){if (Objects.isNull(cell)){return true;}else {if (cell.getCellType() == Cell.CELL_TYPE_BLANK){return true;}else if (cell.getCellType()==Cell.CELL_TYPE_STRING && cell.getStringCellValue().trim().isEmpty()){return true;}else {return false;}}}

二、获取EXCEL的 sheet && 单元格值类型是否合规校验

注-参数说明:

        第一个是mutiple获取的文件字节流,第二个参数是文件类型(xls、xlsx),

        第三个参数是 数据行数的开始索引,第四个参数是数据列数的开始索引,

        第五个参数是不为空的列数字符串,例如:"1,2,3"==》表示第1列、第二列、第三列所属单元格的值不能为空

        第六个参数是一个Map<Integer,Integer>类型,表示第几列单元格的值类型应该为xxx;枚举值是开始的前三行,分别是字符串类型(0),数字类型(1),日期类型(2)

        例如:map.put(1,EXCEL_STRING); ===》表示第一列所属单元格的值应该为字符类型。

    public static final Integer EXCEL_STRING = 0;public static final Integer EXCEL_NUMBER = 1;public static final Integer EXCEL_DATE = 2;public static Sheet getExcelDataSingleSheet(InputStream inputStream,String fileType,Integer rowStartIndex,Integer culStartIndex, String notNullStr,Map<Integer,Integer> classJudgeMap) throws IOException {Workbook workbook = null;if ("xls".equals(fileType)){workbook = new HSSFWorkbook(inputStream);}else if ("xlsx".equals(fileType)){workbook = new XSSFWorkbook(inputStream);}else {throw new IOException("文件类型不存在,请检查!");}//获取第一个sheet页Sheet sheet = workbook.getSheetAt(0);Row rowTitle = null;if (rowStartIndex==1){rowTitle = sheet.getRow(0);}int lastRowNum = sheet.getLastRowNum();for (int i = rowStartIndex; i <= lastRowNum; i++) {Row row = sheet.getRow(i);int lastCellNum = (int)row.getLastCellNum();for (int j = culStartIndex; j < lastCellNum; j++) {Cell cell = row.getCell(j);if (!notNullStr.isEmpty() && notNullStr.contains(String.valueOf(j)) && poiCellIsNull(cell)){StringBuffer stringBuffer = new StringBuffer();stringBuffer.append(String.format("第%d行,第%d列",i+1,j+1));if (rowStartIndex==1){String title = rowTitle.getCell(j).getStringCellValue();stringBuffer.append(String.format("【%s】",title));}stringBuffer.append("不准为空");throw new IOException(stringBuffer.toString());}StringBuffer errorMessage = new StringBuffer();try {if (classJudgeMap.containsKey(j)){switch (classJudgeMap.get(j)){case 0:errorMessage.append(String.format("第%d行,第%d列",i+1,j+1));errorMessage.append(String.format("【%s】",rowTitle.getCell(j).getStringCellValue()));errorMessage.append("应为字符类型");String stringCellValue = cell.getStringCellValue();break;case 1:errorMessage.append(String.format("第%d行,第%d列",i+1,j+1));errorMessage.append(String.format("【%s】",rowTitle.getCell(j).getStringCellValue()));errorMessage.append("应为数值类型");double numericCellValue = cell.getNumericCellValue();break;case 2:errorMessage.append(String.format("第%d行,第%d列",i+1,j+1));errorMessage.append(String.format("【%s】",rowTitle.getCell(j).getStringCellValue()));errorMessage.append("应为日期类型");Date dateCellValue = cell.getDateCellValue();break;default:break;}}}catch (IllegalStateException e){throw new IOException(errorMessage.toString());}catch (Exception e){e.printStackTrace();}}}return sheet;}

http://www.lryc.cn/news/617351.html

相关文章:

  • 【数据库】如何使用一款轻量级数据库SqlSugar进行批量更新,以及查看最终的Sql操作语句
  • Flink TableAPI 按分钟统计数据量
  • MSE ZooKeeper:Flink高可用架构的企业级选择
  • 容器之王--Docker的安全优化详解及演练
  • 【96页PPT】华为IPD流程管理详细版(附下载方式)
  • ARM汇编
  • word格式设置-论文写作,样式,字号等
  • 【大模型】强化学习算法总结
  • 用户管理系统后台管理界面
  • Python面试题及详细答案150道(41-55) -- 面向对象编程篇
  • VBA即用型代码手册:计算选择的单词数Count Words in Selection
  • 5种无需USB线将照片从手机传输到笔记本电脑的方法
  • vue+flask基于规则的求职推荐系统
  • Spring Boot启动事件详解:类型、监听与实战应用
  • 腾讯云Edgeone限时免费
  • 有序矩阵中第K小的元素+二分查找
  • 使用 Rust 创建 32 位 DLL 的完整指南
  • 数据大集网:精准获客新引擎,助力中小企业突破推广困局
  • CSPOJ:1561: 【提高】买木头
  • 请求报文和响应报文(详细讲解)
  • nomachine的安装和使用
  • 零基础学习jQuery第三天
  • 用 Python 绘制企业年度财务可视化报告 —— 从 Excel 到 9 种图表全覆盖
  • DDIA第五章:分布式数据复制中的一致性与冲突处理
  • 第5节 大模型分布式推理通信优化与硬件协同
  • 在Debian上安装MySQL
  • Excel 实战:基因表达矩阵前处理中测序符号的快速剥离方法
  • golang 基础案例_02
  • 设计模式笔记_结构型_享元模式
  • 深入解析Prompt缓存机制:原理、优化与最佳实践