当前位置:网站首页>Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
Import an excel file, solve the problem of skipping blank cells without reading and moving the subscript forward, and return_ BLANK_ AS_ Null red
2022-06-28 03:22:00 【Programming Xiaobai who loves Java】

Problem description : In the use of Poi Import Excel When you file , Like the first line After reading That's ok Traverse after data Column When the license plate number of the trailer is empty during data processing , No data was read from this cell , Because this cell is spatiotemporal , Therefore, the subscript of the data in the next column is moved forward by one , As a result, the field name and content cannot be matched , Some fields in the business have regular judgments , So blank cells must also be read out and assigned blank values , In this way, the fields and contents can be matched one by one , Regular can also pass smoothly .
Here is the main code
First look at the code before the modification ,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
// Get the first Sheet page
Sheet sheet = workbook.getSheetAt(0);
// An iterator method to get rows
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
// Get each column in each row
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;
}Modified code ,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
// Get the first Sheet page
Sheet sheet = workbook.getSheetAt(0);
// // An iterator method to get rows
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
// Get each column in each row
Iterator<Cell> cellIterator = row.cellIterator();
// Count . Control jumps out of the loop . Loop read column
int i = -1;
// Get the number of columns per row
short lastCellNum = row.getLastCellNum();
while (cellIterator.hasNext()) {
i++;
// Can read empty cells normally
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;
}The above is the solution code .
In solving this problem , Thinking about POI Find it on the official website We will see such a solution later
// 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
}
}
}I don't use it because I don't want to change the code I wrote before , So I didn't write it , But see this code :
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);All of a sudden there was inspiration , You can transplant this code into my code , How RETURN_BLANK_AS_NULL Keep reporting red , Do you know that ?
So I have this code
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);It's a good solution to this problem .
So far, the problem has been basically solved !
边栏推荐
- 简单ELK配置实现生产级别的日志采集和查询实践
- adb双击POWER键指令
- [today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper
- Notepad++--列编辑模式--用法/实例
- Summary of software testing tools in 2021 - fuzzy testing tools
- js清空对象和对象的值:
- 新手开哪家的证券账户是比较好?炒股开户安全吗
- 华为设备WLAN基本业务配置命令
- 2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作
- 剑指 Offer 53 - I. 在排序数组中查找数字 I(改进二分)
猜你喜欢

Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队

Gateway微服務路由使微服務靜態資源加載失敗

Arduino esp8266 web LED control

Artifact for converting pcap to JSON file: joy (installation)

如何获取GC(垃圾回收器)的STW(暂停)时间?

被校园暴力,性格内向的马斯克凄惨而励志的童年

A16z:元宇宙解锁游戏基础设施中的新机遇
![[iptables & ICMP] description of ICMP Protocol in iptables default policy](/img/9d/85027ea0b0bc9c6494ba41daed9f38.png)
[iptables & ICMP] description of ICMP Protocol in iptables default policy

嵌入式软件开发中必备软件工具

劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)
随机推荐
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper
Is your IOT security strong enough?
Tardigrade:Trino 解决 ETL 场景的方案
目标检测|SSD原理与实现
业内首个!可运行在移动设备端的视频画质主观体验MOS分评估模型!
__getitem__和__setitem__
How to judge that the thread pool has completed all tasks?
apache、iis6、ii7独立ip主机屏蔽拦截蜘蛛抓取(适用vps云主机服务器)
访问网站提示:您未被授权查看该页恢复办法
Notepad++--常用的插件
Apache——阿帕奇简介
没错,是水的一篇
Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
剑指 Offer 47. 礼物的最大价值(DP)
无代码软件发展简史及未来趋势
华为设备WLAN基本业务配置命令
Tencent games released more than 40 products and projects, including 12 new games
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
剑指 Offer 49. 丑数(三指针法)
【小程序】使用font-awesome字体图标的解决文案(图文)