当前位置:网站首页>导入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);很好的解决了这个问题。
到这里问题基本已经解决!
边栏推荐
- What are the technologies to be mastered in the test? Database design for software testing
- How to judge that the thread pool has completed all tasks?
- 导致系统性能失败的十个原因
- [today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
- 云成本优化有哪些优秀实践?
- 剑指 Offer 49. 丑数(三指针法)
- 元宇宙标准论坛成立
- R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022
- 一位博士在华为的22年(干货满满)
- More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
猜你喜欢

无代码软件发展简史及未来趋势
![[plug in -statistical] statistics the number of code lines and related data](/img/84/ad5e78f7e0ed86d9c21cabe97b9c8e.png)
[plug in -statistical] statistics the number of code lines and related data

为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?

2022安全员-C证考试题库模拟考试平台操作

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

Summary of software testing tools in 2021 - fuzzy testing tools

一位博士在华为的22年(干货满满)

A16z:元宇宙解锁游戏基础设施中的新机遇

How to judge that the thread pool has completed all tasks?

ETCD数据库源码分析——集群间网络层服务端RaftHandler
随机推荐
[issue 21] face to face experience of golang engineer recruited by Zhihu Society
PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
Raspberry pie - environment settings and cross compilation
访问网站提示:您未被授权查看该页恢复办法
读书,使人内心宁静
暴雨去哪儿?天气预报不准谁的锅?
建立自己的网站(17)
Feign远程调用fallback回调失败,无效果
简单ELK配置实现生产级别的日志采集和查询实践
网上股票投资交流群安全吗?进群免费开户靠谱嘛?
Basic flask: template rendering + template filtering + control statement
Dataloader参数collate_fn的使用
Initial linear regression
What are the technologies to be mastered in the test? Database design for software testing
Simple elk configuration to realize production level log collection and query practice
新手开哪家的证券账户是比较好?炒股开户安全吗
买股票通过券商经理的开户链接开户资金是否安全?想开户炒股
ETCD数据库源码分析——集群间网络层服务端RaftHandler
Built in functions for MySQL database operations
Ten reasons for system performance failure