当前位置:网站首页>导入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);很好的解决了这个问题。
到这里问题基本已经解决!
边栏推荐
- Single page application (SPA) hash route and historical API route
- 剑指 Offer 49. 丑数(三指针法)
- ARM Development Studio build编译报错
- Review the submission of small papers for 2022 spring semester courses
- Apache, IIS6 and ii7 independent IP hosts screen and intercept spider crawling (applicable to VPS virtual machine servers)
- 栈的基本操作(C语言实现)
- 【iptables&icmp】iptables默认策略中关于icmp协议的说明
- Summary of software testing tools in 2021 - fuzzy testing tools
- 简单ELK配置实现生产级别的日志采集和查询实践
- Mysql database operation - stored procedure, view, transaction, index, database backup
猜你喜欢

Simple file transfer protocol TFTP

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

元宇宙标准论坛成立

ETCD数据库源码分析——集群间网络层服务端RaftHandler

剑指 Offer 49. 丑数(三指针法)
![[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code](/img/ef/a26127284fe57ac049a4313d89cf97.png)
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code

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

【iptables&icmp】iptables默认策略中关于icmp协议的说明

您的物联网安全性是否足够强大?

Artifact for converting pcap to JSON file: joy (installation)
随机推荐
Domain Name System
Simple elk configuration to realize production level log collection and query practice
Mixed programming of C language and assembly language in stm32
Is it safe to buy stocks and open an account through the account opening link of the broker manager? Want to open an account for stock trading
Single page application (SPA) hash route and historical API route
What are the good practices of cloud cost optimization?
JDBC and MySQL databases
Gateway微服務路由使微服務靜態資源加載失敗
Simple elk configuration to realize production level log collection and query practice
"Everyday Mathematics" serial 53: February 21
Usage details of staticlayout
测试要掌握的技术有哪些?软件测试必懂的数据库设计大全篇
How fiddle uses agents
What is the best and safest software to download when buying stocks?
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
剑指 Offer 47. 礼物的最大价值(DP)
Is your IOT security strong enough?
多快好省,低门槛AI部署工具FastDeploy测试版来了!
Is online stock investment exchange group safe? Is it reliable to open an account for free?
剑指 Offer 49. 丑数(三指针法)