读取的问价格式如下:

直接上代码:
controller层

 @ApiOperation(value = "全自动导入资源和编目")@PostMapping("/autoExcelToSql")public Response readExcelToList(@RequestPart("file") MultipartFile file) throws IOException, BizException {String filename = file.getOriginalFilename();Assert.isNull(filename, "无法获取文件信息");Assert.isTrue(!filename.contains(".xls"), "上传文件类型错误");// 获取文件输入流InputStream inputStream = file.getInputStream();// 获取excel工作簿对象List<AutoExcelToSqlDTO> tableInfos = new ArrayList<>();String substring = filename.substring(filename.lastIndexOf("."), filename.length());if (".xls".equals(substring)) {HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());tableInfos = rdResourceDirService.excelToList(workbook);} else if (".xlsx".equals(substring)) {XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());//tableInfos = excelxToList(workbook);}// 关闭输入流log.info("tableInfos=" + tableInfos);inputStream.close();return Response.success(tableInfos);}

service层

 /*** 文件是.xls** @param workbook* @return*/@Overridepublic List<AutoExcelToSqlDTO> excelToList(HSSFWorkbook workbook) throws BizException {// 得到excel工作表对象HSSFSheet sheetAt = workbook.getSheetAt(0);List<AutoExcelToSqlDTO> autoExcelToSqlDTOS = new ArrayList<>();// 负载行数for (int i = 0; i < sheetAt.getLastRowNum() + 1; i++) {// 获取行数Row row = sheetAt.getRow(i);//首行(即表头)不读取if (row != null) {if (row.getRowNum() == 0 || row.getRowNum() == 1 || row.getRowNum() == 2) {continue;}int ii = i + 1;//读取当前行中单元格数据,索引从0开始// 序号double number = 0;if (row.getCell(0) != null) {row.getCell(0).setCellType(NUMERIC);number = row.getCell(0).getNumericCellValue();}// 资源名称String resName = null;if (row.getCell(1) != null) {row.getCell(1).setCellType(STRING);resName = row.getCell(1).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(resName), "序号为" + ii + "行的数据缺失资源名称参数!");// 归集状态String collectionSituation = null;int collectionSituationInt;if (row.getCell(2) != null) {row.getCell(2).setCellType(STRING);collectionSituation = row.getCell(2).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(collectionSituation), "序号为" + ii + "行的数据缺失归集状态参数!");Assert.isTrue(!collectionSituation.equals("未归集") && !collectionSituation.equals("已归集"), "序号为" + ii + "行的数据归集状态参数格式错误!");if (collectionSituation.equals("未归集")) {collectionSituationInt = 1;} else {collectionSituationInt = 2;}// 更新周期String updateFrequency = null;Integer updateFrequencyInt = null;if (row.getCell(3) != null) {row.getCell(3).setCellType(STRING);updateFrequency = row.getCell(3).getStringCellValue();}if (StringUtil.isNotBlank(updateFrequency)) {switch (updateFrequency) {case "实时":updateFrequencyInt = 1;break;case "每小时":updateFrequencyInt = 2;break;case "每天":updateFrequencyInt = 3;break;case "每月":updateFrequencyInt = 4;break;case "每季度":updateFrequencyInt = 5;break;case "每半年":updateFrequencyInt = 6;break;case "每年":updateFrequencyInt = 7;break;case "不更新":updateFrequencyInt = 8;break;case "不定时":updateFrequencyInt = 9;break;}}// 是否发布(0-未发布   1-已发布)String isRelease = null;int isReleaseInt = 0;if (row.getCell(4) != null) {row.getCell(4).setCellType(STRING);isRelease = row.getCell(4).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(isRelease), "序号为" + ii + "行的数据缺失发布状态参数!");Assert.isTrue(!isRelease.equals("未发布") && !isRelease.equals("已发布"), "序号为" + ii + "行的数据发布状态参数格式错误!");if (isRelease.equals("已发布")) {isReleaseInt = 1;}// 共享方式String resPower = null;int resPowerInt;if (row.getCell(5) != null) {row.getCell(5).setCellType(STRING);resPower = row.getCell(5).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(resPower), "序号为" + ii + "行的数据缺失共享方式参数!");Assert.isTrue(!resPower.equals("公开") && !resPower.equals("受限"), "序号为" + ii + "行的数据共享方式参数格式错误!");if (resPower.equals("公开")) {resPowerInt = 1;} else {resPowerInt = 2;}// 资源来源(部门名称)String resGetSource = null;if (row.getCell(6) != null) {row.getCell(6).setCellType(STRING);resGetSource = row.getCell(6).getStringCellValue();}// 资源应用范围String resLevel = null;Integer resLevelInt = null;if (row.getCell(7) != null) {row.getCell(7).setCellType(STRING);resLevel = row.getCell(7).getStringCellValue();}if (StringUtil.isNotBlank(resLevel)) {switch (resLevel) {case "省级":resLevelInt = 1;break;case "市级":resLevelInt = 2;break;case "区级":resLevelInt = 3;break;}}// 提供部门(id)double resSourceDeptId = 0;if (row.getCell(9) != null) {row.getCell(9).setCellType(NUMERIC);resSourceDeptId = row.getCell(9).getNumericCellValue();}Assert.isTrue(resSourceDeptId == 0, "序号为" + ii + "行的数据缺失部门id参数!");// 资源说明String rdResourceDesc = null;if (row.getCell(10) != null) {row.getCell(10).setCellType(STRING);rdResourceDesc = row.getCell(10).getStringCellValue();}// 处理基本信息AutoExcelToSqlDTO autoExcelToSqlDTO = new AutoExcelToSqlDTO().setResName(resName).setCollectionSituation(collectionSituationInt).setUpdateFrequency(updateFrequencyInt).setIsRelease(isReleaseInt).setResPower(resPowerInt).setResGetSource(resGetSource).setResLevel(resLevelInt).setResSourceDeptId((long) resSourceDeptId).setRdResourceDesc(rdResourceDesc);// 判断是不是合并行int mergerCellRegionRow = getMergerCellRegionRow(sheetAt, row.getRowNum(), 0);i = i + mergerCellRegionRow - 1;// 读取编目信息List<AutoResDataItemDTO> autoResDataItemDTOList = new ArrayList<>();for (int j = row.getRowNum(); j <= i; j++) {Row rowItem = sheetAt.getRow(j);String colName = null;if (rowItem.getCell(11) != null) {colName = rowItem.getCell(11).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(colName), "第"+ j + "行字段中文名不可为空!");String colDesc = null;if (rowItem.getCell(12) != null) {colDesc = rowItem.getCell(12).getStringCellValue();}Assert.isTrue(StringUtil.isBlank(colDesc), "第"+ j + "行字段英文名不可为空!");Assert.isNull(rowItem.getCell(13), "字段类型不能为空");String colType = null;String substring = null;if (rowItem.getCell(13) != null) {colType = rowItem.getCell(13).getStringCellValue();substring = colType.contains("(") ? colType.substring(0, colType.lastIndexOf("(")) : colType;}double colLength = 0.0;if (rowItem.getCell(14) != null) {colLength = rowItem.getCell(14).getNumericCellValue();}double colPrecesion = 0.0;if (rowItem.getCell(15) != null) {colPrecesion = rowItem.getCell(15).getNumericCellValue();}String ifKey = null;int ifKeyInt;if (rowItem.getCell(16) != null) {ifKey = rowItem.getCell(16).getStringCellValue();}if ("yes".equals(ifKey)) {ifKeyInt = 1;} else {ifKeyInt = 2;}AutoResDataItemDTO autoResDataItemDTO = new AutoResDataItemDTO().setChineseName(colDesc).setEnglishName(colName).setFieldType(substring).setFieldLength(colLength + "").setFieldAccuracy(colPrecesion + "").setKeywords(ifKeyInt);autoResDataItemDTOList.add(autoResDataItemDTO);}// 整合参数autoExcelToSqlDTO.setAutoResDataItemDTOList(autoResDataItemDTOList);autoExcelToSqlDTOS.add(autoExcelToSqlDTO);}}return autoExcelToSqlDTOS;}
 /*** 判断单元格是否是合并的单格,如果是,获取其合并的行数。** @param sheet 表单* @param cellRow 被判断的单元格的行号* @param cellCol 被判断的单元格的列号* @return* @throws IOException*/private static int getMergerCellRegionRow(HSSFSheet sheet, int cellRow, int cellCol) {int retVal = 0;int sheetMergerCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergerCount; i++) {CellRangeAddress cra = (CellRangeAddress) sheet.getMergedRegion(i);int firstRow = cra.getFirstRow(); // 合并单元格CELL起始行int firstCol = cra.getFirstColumn(); // 合并单元格CELL起始列int lastRow = cra.getLastRow(); // 合并单元格CELL结束行int lastCol = cra.getLastColumn(); // 合并单元格CELL结束列if (cellRow >= firstRow && cellRow <= lastRow) { // 判断该单元格是否是在合并单元格中if (cellCol >= firstCol && cellCol <= lastCol) {retVal = lastRow - firstRow + 1; // 得到合并的行数break;}}}return retVal;}

******优化字段获取写法

/*** 文件是.xlsx** @param workbook* @return*/@Overridepublic List<AutoExcelToSqlDTO> excelxToList(XSSFWorkbook workbook) throws BizException {// 得到excel工作表对象XSSFSheet sheetAt = workbook.getSheetAt(0);List<AutoExcelToSqlDTO> autoExcelToSqlDTOS = new ArrayList<>();// 负载行数for (int i = 0; i < sheetAt.getLastRowNum() + 1; i++) {// 获取行数Row row = sheetAt.getRow(i);//首行(即表头)不读取if (row != null) {if (row.getRowNum() == 0 || row.getRowNum() == 1 || row.getRowNum() == 2) {continue;}int ii = i + 1;//读取当前行中单元格数据,索引从0开始// 序号
//                double number = getDoubleCellValue(row.getCell(0));
//                if (row.getCell(0) != null) {//                    row.getCell(0).setCellType(NUMERIC);
//                    number = row.getCell(0).getNumericCellValue();
//                }// 资源名称String resName = getStringCellValue(row.getCell(1));Assert.isBlank(resName, "序号为" + ii + "行的数据缺失资源名称参数!");// 归集状态String collectionSituation = getStringCellValue(row.getCell(2));int collectionSituationInt;Assert.isBlank(collectionSituation, "序号为" + ii + "行的数据缺失归集状态参数!");Assert.isTrue(!collectionSituation.equals("未归集") && !collectionSituation.equals("已归集"), "序号为" + ii + "行的数据归集状态参数格式错误!");if (collectionSituation.equals("已归集")) {collectionSituationInt = 1;} else {collectionSituationInt = 2;}// 更新周期String updateFrequency = getStringCellValue(row.getCell(3));Integer updateFrequencyInt = null;if (StringUtil.isNotBlank(updateFrequency)) {switch (updateFrequency) {case "实时":updateFrequencyInt = 1;break;case "每小时":updateFrequencyInt = 2;break;case "每天":updateFrequencyInt = 3;break;case "每月":updateFrequencyInt = 4;break;case "每季度":updateFrequencyInt = 5;break;case "每半年":updateFrequencyInt = 6;break;case "每年":updateFrequencyInt = 7;break;case "不更新":updateFrequencyInt = 8;break;case "不定时":updateFrequencyInt = 9;break;default:updateFrequencyInt = null;}}// 是否发布(0-未发布   1-已发布)String isRelease = getStringCellValue(row.getCell(4));int isReleaseInt = 0;Assert.isBlank(isRelease, "序号为" + ii + "行的数据缺失资源状态参数!");Assert.isTrue(!isRelease.equals("未发布") && !isRelease.equals("已发布"), "序号为" + ii + "行的数据发布状态参数格式错误!");if (isRelease.equals("已发布")) {isReleaseInt = 1;}// 共享方式String resPower = getStringCellValue(row.getCell(5));int resPowerInt;Assert.isBlank(resPower, "序号为" + ii + "行的数据缺失共享方式参数!");Assert.isTrue(!resPower.equals("公开") && !resPower.equals("受限"), "序号为" + ii + "行的数据共享方式参数格式错误!");if (resPower.equals("公开")) {resPowerInt = 1;} else {resPowerInt = 2;}// 资源来源(部门名称)String resGetSource = getStringCellValue(row.getCell(6));// 资源应用范围String resLevel = getStringCellValue(row.getCell(7));Integer resLevelInt = null;if (StringUtil.isNotBlank(resLevel)) {switch (resLevel) {case "省级":resLevelInt = 1;break;case "市级":resLevelInt = 2;break;case "区级":resLevelInt = 3;break;default:resLevelInt = null;}}// 提供部门(id)double resSourceDeptId = getDoubleCellValue(row.getCell(9));Assert.isTrue((int) resSourceDeptId == 0, "序号为" + ii + "行的数据缺失部门id参数!");// 资源说明String rdResourceDesc = getStringCellValue(row.getCell(10));// 处理基本信息AutoExcelToSqlDTO autoExcelToSqlDTO = new AutoExcelToSqlDTO().setResName(resName).setCollectionSituation(collectionSituationInt).setUpdateFrequency(updateFrequencyInt).setIsRelease(isReleaseInt).setResPower(resPowerInt).setResGetSource(resGetSource).setResLevel(resLevelInt).setResSourceDeptId((long) resSourceDeptId).setRdResourceDesc(rdResourceDesc);// 判断是不是合并行int mergerCellRegionRow = getMergerCellRegionRowx(sheetAt, row.getRowNum(), 0);// 如果是合并行if (mergerCellRegionRow > 0) {i = i + mergerCellRegionRow - 1;}// 读取编目信息List<AutoResDataItemDTO> autoResDataItemDTOList = new ArrayList<>();for (int j = row.getRowNum(); j <= i; j++) {Row rowItem = sheetAt.getRow(j);// 英文名称String colDesc = getStringCellValue(rowItem.getCell(12));// 如果英文字段不是空,则说明这行有编目信息需要导入if (StringUtil.isNotBlank(colDesc)) {String colName = getStringCellValue(rowItem.getCell(11));// 字段类型String colType = getStringCellValue(rowItem.getCell(13));Assert.isBlank(colType, "字段类型不能为空,行" + (j + 1));String substring = colType.contains("(") ? colType.substring(0, colType.lastIndexOf("(")) : colType;// 字段长度double colLength = getDoubleCellValue(rowItem.getCell(14));// 字段精度double colPrecesion = getDoubleCellValue(rowItem.getCell(15));String ifKey = getStringCellValue(rowItem.getCell(16));int ifKeyInt;if ("yes".equals(ifKey)) {ifKeyInt = 1;} else {ifKeyInt = 2;}AutoResDataItemDTO autoResDataItemDTO = new AutoResDataItemDTO().setChineseName(colName).setEnglishName(colDesc).setFieldType(substring).setFieldLength(colLength + "").setFieldAccuracy(colPrecesion + "").setKeywords(ifKeyInt);autoResDataItemDTOList.add(autoResDataItemDTO);// 整合参数autoExcelToSqlDTO.setAutoResDataItemDTOList(autoResDataItemDTOList);}}autoExcelToSqlDTOS.add(autoExcelToSqlDTO);}}return autoExcelToSqlDTOS;}

java使用poi实现读取复杂Excel文件相关推荐

  1. html 查看excel表格,html展示excel表格数据-html读取本地excel文件并展示

    怎么用html实现excel表格 首先html不能读取本地excel文件 其次就算是javascript 也是不允许的 这是为了安全考虑 如果前端脚本可以读取本地文件 那很不安全 如何将excel上的 ...

  2. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  3. Java读取批量Excel文件

    Java读取批量Excel文件,并转化为List<Map<String,String>> 1.首先基础知识: 一 :简介 开发中经常会设计到excel的处理,如导出Excel, ...

  4. Java基础系列19:使用JXL或者POI生成和解析Excel文件

    一 简介 如题所示,当我们需要在Java中解析Excel文件时,可以考虑使用JXL或POI的API来解析. 二者的区别如下: jxl现在基本上没被维护了,最近一次更新时间还是几年前.相反,poi属于A ...

  5. java根据Freemarker模板渲染出Excel文件并在浏览器中下载

    **java根据Freemarker模板渲染出Excel文件并在浏览器中下载** 准备工作 1.导入的依赖 2.创建模板 Freemrker语法大全: [Freemarker语法使用请点击该链接跳转学 ...

  6. python使用xlrd读取xlsx文件_005:【Python读取本地Excel文件】使用xlrd模块来读取本地Excel文件...

    使用xlrd模块来读取xls文件 注意:xlrd只能对Excel文件进行"读"操作 1.首先本地新建一个Excel表格(.xls格式),假设表格内容如下: 2.引入xlrd模块(没 ...

  7. java使用POI jar包读写xls文件

    主要使用poi jar来操作excel文件.代码中用到的数据库表信息见ORACLE之表.使用public ArrayList<Person> getPersonAllRecords()获得 ...

  8. matlab读取txt/excel文件并画图的方法

    matlab读取txt/excel文件为矩阵,并进行画图的方法 1. matlab准备 如果没有matlab的话,只需要到学校网站上下载,按照步骤就可以安装.THU的下载地址如下: THU matla ...

  9. tp5 读取/下载 excel文件内容

    tp5 读取/下载 excel文件内容 1.下载一个excel模板 导出函数 /*** 创建(导出)Excel数据表格* @param array $expTableData 要导出的数组格式的数据* ...

最新文章

  1. Red Hat 更新yum源为centos,并安装c环境
  2. AD 修改密码返回错误 Set-ADAccountPassword : 从服务器返回了一个参照。
  3. 【数据库】13种会导致索引失效语句写法
  4. 推荐一系列优秀的Android开发源码
  5. 遗传算法入门到掌握(二)
  6. PHP中stripslashes函数的小结
  7. 针织物染色常见的6大问题
  8. 算法学习_简单递归算法
  9. 《不只是美:信息图表设计原理与经典案例》—— 第1章 为何需要可视化:信息到智慧的升华...
  10. 【收藏】夜光遥感数据下载网址及方式
  11. java二重积分_用java实现二重积分的计算
  12. yarn install出现异常 error An unexpected error occurred: “EIO: i/o error
  13. Android patchrom 流程详解
  14. Python操作网页
  15. 【推荐】jquery开发的大型web应用—H5编辑器工具
  16. 防止电子元器件烧坏那些要避的坑
  17. ZOJ4105 Abbreviation
  18. 什么邮箱群发进箱率高,邮箱怎么群发邮件进箱率怎么样?
  19. C++之operator关键字作用(一百一三十)
  20. 一句话,让你一生不生气!

热门文章

  1. etcd defrag bug [3.1.4] 之前通病
  2. PAS 2050 英文/中文版PDF
  3. 自媒体视频剪辑去哪里学?个人经验分享
  4. 左成云算法进阶班(1)-KMP (python)
  5. VMware16安装Mac11.1Big Sur遇到“客户机操作系统已禁用 CPU。请关闭或重置虚拟机。”解决方案
  6. iphone立体矢量图标_学习教程
  7. pdc是什么部门_pdc是什么意思啊?
  8. Understand简明分析
  9. 博士30万年薪!60万安家费,110万房补,事业编!
  10. vue3 vite学习笔记 YYDS