当前位置:网站首页>导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
导入Excel文件,解决跳过空白单元格不读取,并且下标前移的问题,以及RETURN_BLANK_AS_NULL报红
2022-06-28 02:17:00 【热爱Java的编程小白】

问题描述: 在使用Poi导入Excel文件时,比如第一行 在读取完行数据之后去遍历列数据时遇到挂车的车牌号为空时,没有读取到这个单元格的数据,因为这个单元格时空的,所以给跳过了直接将后一列的数据的下标往前移了一个,导致字段名和内容对不上,在业务中有些字段有正则判断,所以空白单元格必须也要读出来并且赋空值,这样字段和内容才能一一匹配,正则也可以顺利通过。
这里就放主要代码了
先看修改之前的代码,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
//获取第一张Sheet页
Sheet sheet = workbook.getSheetAt(0);
//获取行的一个迭代器方法
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
//获取每行中的每一列
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell == null || cell.getCellType().equals(CellType.BLANK)) {
cell.setCellValue("");
}
Object user = getCellData(cell, workbook);
rowData.add(user);
}
lists.add(rowData);
}
workbook.close();
return lists;
}修改之后的代码,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
//获取第一张Sheet页
Sheet sheet = workbook.getSheetAt(0);
// //获取行的一个迭代器方法
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
//获取每行中的每一列
Iterator<Cell> cellIterator = row.cellIterator();
//计数.控制跳出循环.循环读取列
int i = -1;
//获取每行的列数
short lastCellNum = row.getLastCellNum();
while (cellIterator.hasNext()) {
i++;
//能正常读取空单元格
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (i == lastCellNum) {
break;
}
if (cell == null || cell.getCellType().equals(CellType.BLANK)) {
cell.setCellValue("");
}
Object user = getCellData(cell, workbook);
rowData.add(user);
}
lists.add(rowData);
}
workbook.close();
return lists;
}以上就是解决代码。
在解决这个问题的时候,想着在POI官网上找找 后面就看到这样的一个解决办法
// Decide which rows to process
int rowStart = Math.min(15, sheet.getFirstRowNum());
int rowEnd = Math.max(1400, sheet.getLastRowNum());
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
Row r = sheet.getRow(rowNum);
if (r == null) {
// This whole row is empty
// Handle it as needed
continue;
}
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
for (int cn = 0; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
}
}
}之所以不用是因为不想改动之前写的代码,所以就没照写,但是看到这段代码:
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);突然就有了灵感,可以把这段代码移植到我的代码里面,奈何RETURN_BLANK_AS_NULL一直报红,有大佬知道吗?
所以就有了我这个代码
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);很好的解决了这个问题。
到这里问题基本已经解决!
边栏推荐
- 音视频技术开发周刊 | 251
- Usage details of staticlayout
- Single page application (SPA) hash route and historical API route
- ETCD数据库源码分析——集群间网络层服务端RaftHandler
- [today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
- CI & CD 不可不知!
- Tencent games released more than 40 products and projects, including 12 new games
- 目标检测|SSD原理与实现
- 项目实战!手把手教你 Jmeter 性能测试
- CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
猜你喜欢

微信小程序中生成二维码

How to judge that the thread pool has completed all tasks?
![[iptables & ICMP] description of ICMP Protocol in iptables default policy](/img/9d/85027ea0b0bc9c6494ba41daed9f38.png)
[iptables & ICMP] description of ICMP Protocol in iptables default policy

音视频技术开发周刊 | 251
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
![[today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online](/img/d5/4b3e622ab77bc546ca5d285ef67d8a.jpg)
[today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online

【插件-statistic】统计代码行数和相关数据

项目实战!手把手教你 Jmeter 性能测试

R language penalty logistic regression, linear discriminant analysis LDA, generalized additive model GAM, multiple adaptive regression splines Mars, KNN, quadratic discriminant analysis QDA, decision
随机推荐
2-5 basic configuration -win2003 add attack surface
2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
【iptables&icmp】iptables默认策略中关于icmp协议的说明
s32ds跳转到DefaultISR
Mixed programming of C language and assembly language in stm32
数字化时代,企业须做好用户信息安全
Apache, IIS6 and ii7 independent IP hosts screen and intercept spider crawling (applicable to VPS virtual machine servers)
项目实战!手把手教你 Jmeter 性能测试
Flask Foundation: template inheritance + static file configuration
apache、iis6、ii7独立ip主机屏蔽限制ip访问
Gateway microservice routing failed to load microservice static resources
Writing based on stm32
Tardigrade:Trino 解决 ETL 场景的方案
Domain Name System
Redis搭建集群【简单】
Review the submission of small papers for 2022 spring semester courses
2022安全员-C证考试题库模拟考试平台操作
空闲中断无法清除
分布式事务—基于消息补偿的最终一致性方案(本地消息表、消息队列)