String path = "whitelist.xlsx";
Workbook wb = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
wb = WorkbookFactory.create(fis);
} catch (EncryptedDocumentException | InvalidFormatException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException("System hault,can't open file:"+path);
}
//get first sheet
Sheet sheet = wb.getSheetAt(0);
int first = sheet.getFirstRowNum();
int last = sheet.getLastRowNum();
//traverse rows
for(int i=first; i<=last;i++){
//read row data
Row row = sheet.getRow(i);
if(row == null){
continue;
}
//read first cell
Cell cell = row.getCell(0);
if(null == cell){
continue;
}
//only read STRING & INTEGER
String content = null;
if(cell.getCellType() == Cell.CELL_TYPE_STRING){
content = cell.getStringCellValue();
}else if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
content = cell.getNumericCellValue() + "";
}else {
continue;
}
//do something...
}
try {
//close stream and workbook
wb.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}