我正在使用Apache POI讀取Excel文件。 我的Excel表格的結構是這樣的在單列中讀取包含多個值的Excel文件-Java
|2000s| 2001, 2003, 2008, 2009|
所以右側的數據,我需要它分配給2000
到現在我已經實現了這種方式:
List<Class> list = new ArrayList<Class>();
File file = new File(file_path);
FileInputStream fis = new FileInputStream(file);
//Create an instance of workbook which refers to an excel file
XSSFWorkbook wb = new XSSFWorkbook(fis);
//This selects the 1st sheet
XSSFSheet sheet = wb.getSheetAt(0);
//Iterate through each row one by one
Iterator<Row> itr = sheet.iterator();
String newName = null;
String oldName = null;
while(itr.hasNext()){
Row nextRow = itr.next();
// For each row, iterate through all the columns
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
newName = nextRow.getCell(0).toString();
if(nextRow.getCell(1).toString().contains(",")){
StringTokenizer st = new StringTokenizer(nextRow.getCell(1).toString(),",");
while(st.hasMoreTokens()){
oldName = st.nextToken();
}
}
else{
oldName = nextRow.getCell(1).toString();
}
}
System.out.println();
}
當我編譯時,它會在nextRow.getCell(1)
行處引發「空指針異常」。
我不明白我該如何映射所有逗號值到2000年。
這對於普通數據(沒有逗號)是完全正常的。