当前位置:网站首页>导入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);很好的解决了这个问题。
到这里问题基本已经解决!
边栏推荐
- 简单ELK配置实现生产级别的日志采集和查询实践
- Simple elk configuration to realize production level log collection and query practice
- 2021年软件测试工具总结——模糊测试工具
- Reading makes people quiet
- apache、iis6、ii7独立ip主机屏蔽限制ip访问
- 数字化时代,企业须做好用户信息安全
- 将PCAP转换为Json文件的神器:joy(安装篇)
- Ten reasons for system performance failure
- 2-5 basic configuration -win2003 add attack surface
- ADB double click the power key command
猜你喜欢

Dataloader参数collate_fn的使用

Raspberry pie - environment settings and cross compilation
![[postgraduate] bit by bit](/img/76/b804ff215b8f52f1fe603a9a34a352.jpg)
[postgraduate] bit by bit

微信小程序中生成二维码

【Kotlin】在Android官方文档中对其语法的基本介绍和理解

剑指 Offer 49. 丑数(三指针法)

【活动早知道】LiveVideoStack近期活动一览

喜新厌旧?IT公司为什么宁愿花20k招人,也不愿涨薪留住老员工

More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!

一位博士在华为的22年(干货满满)
随机推荐
Apache - about Apache
Is online stock investment exchange group safe? Is it reliable to open an account for free?
Tips for visiting the website: you are not authorized to view the recovery method of this page
Simple elk configuration to realize production level log collection and query practice
Apache——阿帕奇簡介
剑指 Offer 47. 礼物的最大价值(DP)
Arduino esp8266 web LED control
Writing based on stm32
华为设备WLAN基本业务配置命令
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
无代码软件发展简史及未来趋势
Basic flask: template rendering + template filtering + control statement
What are the good practices of cloud cost optimization?
国泰君安证券靠谱吗?开证券账户安全吗?
Mixed programming of C language and assembly language in stm32
PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
Simple file transfer protocol TFTP
A16z: metauniverse unlocks new opportunities in game infrastructure
JS clear the object and its value:
Heartless sword English Chinese bilingual poem 004 Meditation