java cell type blank,java - ExcelReader workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK)无法正常工...

在使用Apache POI读取Excel文件时,遇到单元格缺失的问题,代码设置了`workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK)`,期望空单元格被处理为空白字符串,但实际上并未按预期工作。问题在于如何正确处理缺失的单元格以避免值被错误地放入其他列。

我正在尝试使用apache poi读取excel文件(xls)文件。 在这种情况下,如果在读取行期间缺少某个单元格(cellIterator),则会跳过该单元格并将下一个值放置到其他标头中。

ABC

1 2 3

4空白6

在上述情况下,它在空白单元格的“ B”列中放入6,我需要B作为空白字符串。

`package com.howtodoinjava.demo.poi;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

public class ReadExcelDemo {

Integer rowNum;

Iterator rowIterator;

HSSFWorkbook workbook;

HSSFSheet sheet;

FileInputStream file;

public ReadExcelDemo(File file1) throws IOException{

this.file = new FileInputStream(file1);

// Create Workbook instance holding reference to .xlsx file

this.workbook = new HSSFWorkbook(file);

workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);

// Get first/desired sheet from the workbook

this.sheet = workbook.getSheetAt(0);

}

public static void main(String[] args) throws IOException {

for(int i =0;i<5;i++) {

List rowData = new ReadExcelDemo(new File(

"howtodoinjava_demo_xls.xls")).readRow();

System.out.println(rowData);

}

}

private List readRow() throws IOException {

List rowData = new ArrayList();

// Iterate through each rows one by one

rowIterator = sheet.iterator();

if (getNext()) {

Row row = rowIterator.next();

// For each row, iterate through all the columns

Iterator cellIterator = row.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

// Check the cell type and format accordingly

switch (cell.getCellType()) {

case Cell.CELL_TYPE_NUMERIC:

rowData.add(String.valueOf(cell.getNumericCellValue()));

System.out.print(cell.getNumericCellValue() + "\t");

break;

case Cell.CELL_TYPE_STRING:

rowData.add(cell.getStringCellValue());

System.out.print(cell.getStringCellValue() + "\t");

break;

case Cell.CELL_TYPE_BLANK:

rowData.add("");

System.out.println("");

}

}

System.out.println("");

}

rowNum++;

close();

return rowData;

}

private void close() throws IOException {

file.close();

}

private boolean getNext() {

// TODO Auto-generated method stub

if (null == rowNum) {

rowNum = 0;

}

return rowIterator.hasNext();

}

}

`

这是代码片段。 我尝试了workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK); 但它不起作用。 有什么建议为什么会发生??

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值