excel格式如下:

代码如下:

package com.example.demo.excel;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;public class ExcelTest {public String addReportByExcel(InputStream inputStream, String fileName){String message = "Import success";boolean isE2007 = false;    //格式判断if(fileName.endsWith("xlsx")){isE2007 = true;}int rowIndex = 0;int columnIndex = 0;try {InputStream input = inputStream;  //建立输入流Workbook wb  = null;if(isE2007){wb = new XSSFWorkbook(input);}else{wb = new HSSFWorkbook(input);}Sheet sheet = wb.getSheetAt(0);    //获得第一个表单//System.out.println("总行数:"+sheet.getLastRowNum());List<CellRangeAddress> cras = getCombineCell(sheet);int count = sheet.getLastRowNum()+1;//总行数List<InspectionReport> irs = new ArrayList<>();for(int i = 1; i < count;i++){rowIndex = i;Row row = sheet.getRow(i);InspectionReport ir = new InspectionReport();ir.setReportName(getCellValue(row.getCell(0)));ir.setShift(Double.valueOf(getCellValue(row.getCell(1))).intValue());ir.setLine(getCellValue(row.getCell(2)));ir.setStationCode(getCellValue(row.getCell(3)));ir.setArea(Double.valueOf(getCellValue(row.getCell(4))).intValue());ir.setReportStatus(Double.valueOf(getCellValue(row.getCell(5))).intValue());List<InspectionItem> items = new ArrayList<>();if(isMergedRegion(sheet,i,0)){int lastRow = getRowNum(cras,sheet.getRow(i).getCell(0),sheet);for(;i<=lastRow;i++){row = sheet.getRow(i);InspectionItem item = new InspectionItem();item.setItem(getCellValue(row.getCell(6)));item.setMethod(getCellValue(row.getCell(7)));item.setMode(getCellValue(row.getCell(8)));item.setStandardValue(getCellValue(row.getCell(9)));item.setDeviationValue(getCellValue(row.getCell(10)));String pinci = getCellValue(row.getCell(11));Double d = Double.valueOf(pinci);item.setFrequency(d.intValue());items.add(item);}i--;}else{row = sheet.getRow(i);InspectionItem item = new InspectionItem();item.setItem(getCellValue(row.getCell(6)));item.setMethod(getCellValue(row.getCell(7)));item.setMode(getCellValue(row.getCell(8)));item.setStandardValue(getCellValue(row.getCell(9)));item.setDeviationValue(getCellValue(row.getCell(10)));String pinci = getCellValue(row.getCell(11));Double d = Double.valueOf(pinci);item.setFrequency(d.intValue());items.add(item);}ir.setItems(items);irs.add(ir);}System.out.println(irs);} catch (Exception ex) {return "error"}return message;}/*** 获取单元格的值* @param cell* @return*/public String getCellValue(Cell cell){if(cell == null) return "";if(cell.getCellType() == Cell.CELL_TYPE_STRING){return cell.getStringCellValue();}else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN){return String.valueOf(cell.getBooleanCellValue());}else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){return cell.getCellFormula() ;}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){return String.valueOf(cell.getNumericCellValue());}return "";}/*** 合并单元格处理,获取合并行* @param sheet* @return List<CellRangeAddress>*/public List<CellRangeAddress> getCombineCell(Sheet sheet){List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();//获得一个 sheet 中合并单元格的数量int sheetmergerCount = sheet.getNumMergedRegions();//遍历所有的合并单元格for(int i = 0; i<sheetmergerCount;i++){//获得合并单元格保存进list中CellRangeAddress ca = sheet.getMergedRegion(i);list.add(ca);}return list;}private int getRowNum(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet){int xr = 0;int firstC = 0;int lastC = 0;int firstR = 0;int lastR = 0;for(CellRangeAddress ca:listCombineCell){//获得合并单元格的起始行, 结束行, 起始列, 结束列firstC = ca.getFirstColumn();lastC = ca.getLastColumn();firstR = ca.getFirstRow();lastR = ca.getLastRow();if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR){if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC){xr = lastR;}}}return xr;}/*** 判断单元格是否为合并单元格,是的话则将单元格的值返回* @param listCombineCell 存放合并单元格的list* @param cell 需要判断的单元格* @param sheet sheet* @return*/public String isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell,Sheet sheet)throws Exception{int firstC = 0;int lastC = 0;int firstR = 0;int lastR = 0;String cellValue = null;for(CellRangeAddress ca:listCombineCell){//获得合并单元格的起始行, 结束行, 起始列, 结束列firstC = ca.getFirstColumn();lastC = ca.getLastColumn();firstR = ca.getFirstRow();lastR = ca.getLastRow();if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR){if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC){Row fRow = sheet.getRow(firstR);Cell fCell = fRow.getCell(firstC);cellValue = getCellValue(fCell);break;}}else{cellValue = "";}}return cellValue;}/*** 获取合并单元格的值* @param sheet* @param row* @param column* @return*/public String getMergedRegionValue(Sheet sheet ,int row , int column){int sheetMergeCount = sheet.getNumMergedRegions();for(int i = 0 ; i < sheetMergeCount ; i++){CellRangeAddress ca = sheet.getMergedRegion(i);int firstColumn = ca.getFirstColumn();int lastColumn = ca.getLastColumn();int firstRow = ca.getFirstRow();int lastRow = ca.getLastRow();if(row >= firstRow && row <= lastRow){if(column >= firstColumn && column <= lastColumn){Row fRow = sheet.getRow(firstRow);Cell fCell = fRow.getCell(firstColumn);return getCellValue(fCell) ;}}}return null ;}/*** 判断指定的单元格是否是合并单元格* @param sheet* @param row 行下标* @param column 列下标* @return*/private boolean isMergedRegion(Sheet sheet,int row ,int column) {int sheetMergeCount = sheet.getNumMergedRegions();for (int i = 0; i < sheetMergeCount; i++) {CellRangeAddress range = sheet.getMergedRegion(i);int firstColumn = range.getFirstColumn();int lastColumn = range.getLastColumn();int firstRow = range.getFirstRow();int lastRow = range.getLastRow();if(row >= firstRow && row <= lastRow){if(column >= firstColumn && column <= lastColumn){return true;}}}return false;}}

实体类如下

package com.example.demo.excel;import lombok.Data;import java.util.List;
@Data
public class InspectionReport {private String reportName;private Integer shift;private String line;private String stationCode;private Integer area;private Integer reportStatus;private List<InspectionItem> items;
}package com.example.demo.excel;import lombok.Data;@Data
public class InspectionItem {private String item;private String method;private String mode;private String standardValue;private String deviationValue;private Integer frequency;
}

pom文件引入poi:

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.14</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version></dependency>

由于上传不了表格,我复制了贴上来给你们

报表名称 班次 生产线 站点编号 设备区域 报表状态 检查项目 检查方法 填报方式 标准值 偏差值 频次
import1 1 lc 123 2 1 检查项目1 目测 数值 1 1 1
检查项目2 填报 文本 2 2 2
检查项目3 目测 数值 3 3 3
import2 2 lc 456 2 1 检查项目4 目测 数值 4 4 4
检查项目5 填报 文本 5 5 5
检查项目6 目测 数值 6 6 6
import3 3 lk 7 2 1 检查项目7 目测 数值 7 7 7
import4 4 lc 789 2 1 检查项目8 目测 数值 8 8 8
检查项目9 填报 文本 9 9 9
检查项目10 目测 数值 10 10 10

java读取含有合并行的excel相关推荐

  1. Java读取、写入、处理Excel文件中的数据

    在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Excel会有 ...

  2. 【EasyExcel】Java读取一维及二维Excel数据并存入数组

    EasyExcel是一个基于Java的.快速.简洁.解决大文件内存溢出的Excel处理工具.它能让你在不用考虑性能.内存的等因素的情况下,快速完成Excel的读.写等功能. Java基于EasyExc ...

  3. python如何读取excel宏_Python读取含有VBA宏的Excel数据

    因为现在项目在开发新的API,我需要根据API方法论去校验这个API的计算逻辑和计算结果是否正确,而且需要将计算逻辑用Excel记录下来,所以就用了Excel的VBA. 1. 什么是VBA? 具体含义 ...

  4. java 读取pdf、word、Excel文件

    用到的jar: itextpdf-5.5.8.jar   (PDF) poi.jar public class FileUtils {/*** 判断文件是否存在* * @Title: isExcite ...

  5. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  6. java poi exce 移动_JAVA_POI 操作Excel

    转自: http://rensanning.iteye.com/blog/1538591# Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API ...

  7. java操作导出Excel(jxl导出WritableWorkbook)jxl合并单元格,单元格的设置,单元格居中、字体、大小、换行、合并行,列宽、自动换行撑起高度、指定特定字符串样式等

    new WritableCellFormat().setWrap(true);//通过调整宽度和高度自动换行 1.1     需求描述 MS的电子表格(Excel)是Office的重要成员,是保存统计 ...

  8. java合并sheet行_java poi Excel循环合并行

    //Java poi 实现循环合并行,还是第一次遇到这种问题 //在网上查了很多资料,都不是自己想要的 //以下为自己研究后,写的一点东西,给大家分享,希望对大家能有思路上的启发,也希望大家能提出宝贵 ...

  9. java 读取excel_Java12POI操作Excel

    Apache POI是一个开源的利用Java读写Excel,WORD等微软OLE2组件文档的项目. 我的需求是对Excel的数据进行导入或将数据以Excel的形式导出. 先上简单的测试代码: pack ...

最新文章

  1. QUARK的增强版C-QUARK问世,有效提升蛋白质结构从头预测精度
  2. R语言caret包构建xgboost模型实战:特征工程(连续数据离散化、因子化、无用特征删除)、配置模型参数(随机超参数寻优、10折交叉验证)并训练模型
  3. corosync+pacemaker+drbd构建mysql高可用平台的简单案例
  4. taro 引入js_Taro跨端开发之多业务模块管理 React Native篇(终篇)
  5. 基于DataTables实现根据每个用户动态显示隐藏列,可排序
  6. python 模拟登陆智联_Python+scrapy爬虫之模拟登陆
  7. leetcode 628. Maximum Product of Three Numbers | 628. 三个数的最大乘积(Java)
  8. 阿里云刘伟光:金融核心系统将步入分布式智能化的时代
  9. EfficientDet论文阅读解析【精】公式纯手打
  10. 教你高效管理CrossOver容器
  11. Winfrom 中如何实现combox 的列表自动显示ToolTip提示
  12. MySQL之用Mysql-Proxy实现读写分离
  13. 6.0新特性 权限管理方式
  14. zabbix3.2短信告警脚本
  15. matlab设置脚本,MATLAB脚本和功能
  16. Ubuntu下Opencv安装与使用
  17. dev 居中_css的div垂直居中的方法,百分比div垂直居中
  18. 大学生面试着装要求(男生篇)
  19. 猿如意中的【取色器】效率工具详情介绍
  20. 用python根据生日判断星座_求指教,我这个 代码是实现 根据生日判断星座

热门文章

  1. 数据预处理之One-Hot(独热编码)编码
  2. Java EE的几个常用框架简介
  3. rsync同步+inotify实时同步部署
  4. javascript-轮播图
  5. 记录Guava版本冲突而出现项目启动失败的问题
  6. Tensorflow③ Keras的LSTM和TF的LSTM实现的源码剖析
  7. 从感知机到Transformer,一文概述深度学习简史
  8. 2022-2028全球氨洗涤器行业调研及趋势分析报告
  9. violate关键字---java高并发
  10. GaussDB数据库管理