当前位置:网站首页>Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
Importer un fichier Excel, résoudre le problème de sauter les cellules vides et de ne pas lire, et avancer l'indice, et retourner Blank As NULL Red
2022-06-28 03:22:00 【Love Java Programming LITTLE WHITE】

Description du problème: En servicePoiImporterExcelAu moment du dossier,Comme la première ligne À la fin de la lectureD'accordAprès les données, traversezColonnesLorsque le numéro de plaque d'immatriculation de la remorque rencontrée au moment des données est vide,Aucune donnée n'a été lue pour cette cellule,Parce que cette cellule est spatio - temporelle,Donc pour sauter l'indice qui déplace directement les données de la dernière colonne vers l'avant d'un,Faire en sorte que le nom et le contenu du champ ne correspondent pas,Certains champs ont un jugement régulier en affaires,Donc les cellules vides doivent aussi être lues et donner des valeurs nulles,Pour que les champs et le contenu correspondent un par un,Le regular peut aussi passer sans heurt.
Voici le Code principal
Regardez d'abord le code avant de le modifier,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
//Obtenir la première feuilleSheet(En milliers de dollars des États - Unis)
Sheet sheet = workbook.getSheetAt(0);
//Une méthode itérative pour obtenir la ligne
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
// Obtenez chaque colonne de chaque ligne
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;
}Code modifié,
Workbook workbook = null;
List<List<Object>> lists = new ArrayList<>();
workbook = new HSSFWorkbook(inputStream);
//Obtenir la première feuilleSheet(En milliers de dollars des États - Unis)
Sheet sheet = workbook.getSheetAt(0);
// //Une méthode itérative pour obtenir la ligne
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
ArrayList<Object> rowData = new ArrayList<>();
Row row = rowIterator.next();
// Obtenez chaque colonne de chaque ligne
Iterator<Cell> cellIterator = row.cellIterator();
//Nombre. Contrôle de la boucle de sortie . Colonne de lecture circulaire
int i = -1;
// Obtient le nombre de colonnes par ligne
short lastCellNum = row.getLastCellNum();
while (cellIterator.hasNext()) {
i++;
// Lire les cellules vides normalement
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;
}Ce qui précède est le Code de résolution .
En résolvant ce problème,En pensant àPOI Cherchez sur le site officiel Une solution comme celle - ci se trouve à l'arrière
// 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
}
}
}Ce n'est pas nécessaire parce que vous ne voulez pas changer le Code que vous avez écrit , Donc il n'y a pas de photo , Mais vu ce code :
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);Et soudain, l'inspiration , Vous pouvez transplanter ce code dans mon code ,Naiho!RETURN_BLANK_AS_NULLToujours en rouge,Y a - t - il des grands qui savent?
Donc avec ce code
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);C'est une bonne solution..
Le problème a été résolu ici !
边栏推荐
- PHP 代码 微信、公众号、企业微信 发送表情符号 [U+1F449]
- [today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code
- 2022电工(初级)复训题库及在线模拟考试
- How to judge that the thread pool has completed all tasks?
- Relative path writing of files
- Gateway微服务路由使微服务静态资源加载失败
- RichView TRVStyle ParaStyles
- 【小游戏】跑酷
- Flow based depth generation model
- 剑指 Offer 49. 丑数(三指针法)
猜你喜欢
![[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

__getitem__和__setitem__

Feign远程调用fallback回调失败,无效果

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

What are the technologies to be mastered in the test? Database design for software testing

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

You got 8K in the 3-year function test, but were overtaken by the new tester. In fact, you are pretending to work hard

be fond of the new and tired of the old? Why do it companies prefer to spend 20K on recruiting rather than raise salaries to retain old employees

Basic operation of stack (implemented in C language)

ETCD数据库源码分析——集群间网络层服务端RaftHandler
随机推荐
Object类,以及__new__,__init__,__setattr__,__dict__
【iptables&icmp】iptables默认策略中关于icmp协议的说明
Mysql database operation - stored procedure, view, transaction, index, database backup
Opencv -- geometric space transformation (affine transformation and projection transformation)
剑指 Offer 49. 丑数(三指针法)
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
Domain Name System
Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
为什么OpenCV计算的帧率是错误的?
Simple elk configuration to realize production level log collection and query practice
Why are so many people keen on big factories because of the great pressure and competition?
一位博士在华为的22年(干货满满)
买股票应该下载什么软件最好最安全?
文件的相对路径写法
基于流的深度生成模型
CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English
Basic flask: template rendering + template filtering + control statement
无代码软件发展简史及未来趋势
论文阅读:Generative Adversarial Transformers
【活动早知道】LiveVideoStack近期活动一览