当前位置:网站首页>導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
導入Excel文件,解决跳過空白單元格不讀取,並且下標前移的問題,以及RETURN_BLANK_AS_NULL報紅
2022-06-28 03:22: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);很好的解决了這個問題。
到這裏問題基本已經解决!
边栏推荐
猜你喜欢

s32ds跳转到DefaultISR
![[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

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

为什么OpenCV计算的帧率是错误的?

2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units

元宇宙标准论坛成立

R1 Quick Open Pressure Vessel Operation Special Operation Certificate Examination Library and Answers in 2022

3年功能测试拿8K,被刚来的测试员反超,其实你在假装努力

劲爆!YOLOv6又快又准的目标检测框架开源啦(附源代码下载)

TensorRT 模型推理优化实现
随机推荐
How to write concise code? (upper)
How to judge that the thread pool has completed all tasks?
Ten reasons for system performance failure
Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
嵌入式DSP音频开发
Artifact for converting pcap to JSON file: joy (installation)
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
2022电工(初级)复训题库及在线模拟考试
空闲中断无法清除
剑指 Offer 49. 丑数(三指针法)
[522. longest special sequence II]
嵌入式软件开发中必备软件工具
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
A16z:元宇宙解锁游戏基础设施中的新机遇
How fiddle uses agents
Packet capturing and sorting out external Fiddler -- understanding the toolbar [1]
网上股票投资交流群安全吗?进群免费开户靠谱嘛?
多快好省,低门槛AI部署工具FastDeploy测试版来了!
TensorRT 模型推理优化实现
2022危险化学品经营单位安全管理人员特种作业证考试题库模拟考试平台操作