当前位置:网站首页>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 !
边栏推荐
- 买股票通过券商经理的开户链接开户资金是否安全?想开户炒股
- Tencent games released more than 40 products and projects, including 12 new games
- 网上股票投资交流群安全吗?进群免费开户靠谱嘛?
- 2022 operation of simulated examination platform of special operation certificate examination question bank for safety management personnel of hazardous chemical business units
- Embedded DSP audio development
- Question bank and answers of special operation certificate for R1 quick opening pressure vessel operation in 2022
- crond BAD FILE MODE /etc/cron.d
- Object类,以及__new__,__init__,__setattr__,__dict__
- 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
- Is it reliable to invest in the inter-bank certificate of deposit fund? Is the inter-bank certificate of deposit fund safe
猜你喜欢

如何编写简洁代码?(上)

collections.defaultdict()的使用

R语言惩罚逻辑回归、线性判别分析LDA、广义加性模型GAM、多元自适应回归样条MARS、KNN、二次判别分析QDA、决策树、随机森林、支持向量机SVM分类优质劣质葡萄酒十折交叉验证和ROC可视化

根据Explain查看sql执行计划,对SQL进行优化
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

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

元宇宙标准论坛成立

Basic operation of stack (implemented in C language)

拾光者,云南白药!

Flow based depth generation model
随机推荐
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper
买股票通过券商经理的开户链接开户资金是否安全?想开户炒股
嵌入式软件开发中必备软件工具
Arduino esp8266 web LED control
What are the technologies to be mastered in the test? Database design for software testing
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
JS clear the object and its value:
论文阅读:Generative Adversarial Transformers
Flask Foundation: template inheritance + static file configuration
2022安全员-C证考试题库模拟考试平台操作
新手开哪家的证券账户是比较好?炒股开户安全吗
"Everyday Mathematics" serial 53: February 21
一位博士在华为的22年(干货满满)
网上股票投资交流群安全吗?进群免费开户靠谱嘛?
Embedded DSP audio development
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
音视频技术开发周刊 | 251
[plug in -statistical] statistics the number of code lines and related data
Built in functions for MySQL database operations