使用java读取excel时将null更改为空
如果我使用的excel工作表的值为空,则此代码会给出一个错误“java.lang.NullPointerException”。有没有办法将空值更改为空?谢谢使用java读取excel时将null更改为空,java,excel,nullpointerexception,null,Java,Excel,Nullpointerexception,Null,如果我使用的excel工作表的值为空,则此代码会给出一个错误“java.lang.NullPointerException”。有没有办法将空值更改为空?谢谢 public class GradesDB { public static void main (String[] args) throws Exception { //initiating workbook File excel = new File("Users/Database.xlsx"); //file lo
public class GradesDB {
public static void main (String[] args) throws Exception {
//initiating workbook
File excel = new File("Users/Database.xlsx"); //file location
FileInputStream fis= new FileInputStream(excel);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet ws = wb.getSheet("Students"); //sheet with null value
int rowNum = ws.getLastRowNum() +1;
int colNum = ws.getRow(0).getLastCellNum();
String[][] data = new String[rowNum][colNum]; //storing row and col
//iteration
for (int i =0; i <rowNum; i++) {
XSSFRow row = ws.getRow(i);
for (int j = 0; j <colNum; j++){
XSSFCell cell = row.getCell(j);
String value = cellToString(cell);
data[i][j] = value;
System.out.println(value);
}
}
//System.out.println(data[4][3]);
}
public static String cellToString(XSSFCell cell) {
int type;
Object result = null;
type = cell.getCellType();
switch (type) {
case 0 :
result = cell.getNumericCellValue(); //numeric value
break;
case 1 :
result = cell.getStringCellValue(); //string value
break;
case 2 :
result = cell.getBooleanCellValue();
break;
}
return result.toString();
}
}
public class-GradesDB{
公共静态void main(字符串[]args)引发异常{
//启动工作簿
File excel=新文件(“Users/Database.xlsx”);//文件位置
FileInputStream fis=新的FileInputStream(excel);
XSSF工作簿wb=新XSSF工作簿(fis);
XSSFSheet ws=wb.getSheet(“学生”);//具有空值的工作表
int rowNum=ws.getLastRowNum()+1;
int colNum=ws.getRow(0.getLastCellNum();
字符串[][]数据=新字符串[rowNum][colNum];//存储行和列
//迭代
对于(int i=0;我在条件中检查null
。if((ws.getLastRowNum()+1)==null){//do something/}else{rowNum=ws.getLastRowNum()+1;}
或使用尝试{}catch(NullPointerException NPE){NPE.printStackTrace();}
谢谢您的评论。