gpt4 book ai didi

java - 从 excel XSSFCell cell = row.getCell(j) 读取时获取空指针异常 java;

转载 作者:行者123 更新时间:2023-11-29 07:04:42 46 4
gpt4 key购买 nike

private String[][] data;

// to read from excel file

public void readFile() throws IOException {

// Path to the excel file
// File datafile = new File("C:\\Users\\atomar\\Documents\test.xlsx");
// A Buffered File Input Stream to read the data
FileInputStream f = new FileInputStream(
"C:\\Users\\atomar\\Documents\\test.xlsx");
System.out.println("file found");
// InputStream fis = new BufferedInputStream(new FileInputStream("f"));
// We create a workbook which represents the excel file
XSSFWorkbook book = new XSSFWorkbook(f);

// Next a sheet which represents the sheet within that excel file
XSSFSheet sheet = book.getSheet("Sheet1");

// No of rows in the sheet
int rowNum = sheet.getLastRowNum() + 1;
System.out.println("rowNum");

// No of columns in the sheet
int colNum = sheet.getRow(0).getLastCellNum();

// A Two dimensional array of Strings which represents the data in the
// sheet
data = new String[rowNum][colNum];
{

for (int i = 0; i < rowNum; i++) {
// Get the row
XSSFRow row = sheet.getRow(i);

for (int j = 0; j < colNum; j++) {
// Get the columns or cells for the first row and keep
// looping
// for the other rows
XSSFCell cell = row.getCell(j);

// Make a call to the method cellToString which actually
// converts the cell contents to String
data[i][j] = cellToString(cell);

// data[i][j] = value;

// Here is where you write the logic to handle the data.I am
// just printing out the contents here.

//System.out.println("The value is " + data[i][j]);


}
}

我在这里调用那个函数

public void InterestedParty() throws InterruptedException {
try {
readFile();
} catch (IOException e1) {
}
}

出现错误:不支持这种类型的单元格我从输入的 excel 文件中删除了两行数据,然后它在工作正常之前就开始了。但是现在我已经完全删除了那些行,还有问题

最佳答案

如果没有数据,XSSFCell 将为null。检查它是否为 null。在调用您的 cellToString() 方法之后。

    XSSFCell cell = row.getCell(j);
if(cell != null) {
data[i][j] = cellToString(cell);
} else {
// if you would like to set value when cell is null
row.createCell(j).setCellValue(your value);
}

关于java - 从 excel XSSFCell cell = row.getCell(j) 读取时获取空指针异常 java;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156383/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com