I believe EXCEL import is no stranger to everyone. The following is an excel import made on my project.
InputStream = new FileInputStream (file );
Workbook workbook;
Sheet sheet;
Row row;
Cell cell;
If (realPath. substring (realPath. lastIndexOf (".") + 1). equals ("xlsx") {// excel2007
Workbook = new XSSFWorkbook (inputStream );
Sheet = workbook. getSheetAt (0 );
} Else {// excel97-2003
Workbook = new HSSFWorkbook (inputStream );
Sheet = workbook. getSheetAt (0 );
}
// -- Header information startY: Valid excel rows endX: Valid excel Columns
Row = sheet. getRow (startY );
Cell = row. getCell (endX );
// -- Table body information
For (int I = startY; I <endY; I ++ ){
Row = sheet. getRow (I );
If (row = null ){
Throw new Exception ("verification error: Row" + (I + 1) + "no data! ");
} Else {
For (int j = startX; j <endX; j ++) {// -- startX: cells read from excel
Cell = row. getCell (j );
If (cell = null ){
Throw new Exception ("verification error: column" + (I + 1) + "row" + (j + 1) + "no data! ");
} Else {
If (j = startX ){
If (cell. CELL_TYPE_BLANK = cell. getCellType ()){
Throw new Exception ("verification error: column xx does not exist." + (I + 1) + "row" + (j + 1) +! ");
} Else {
.......
}
}
}
}
}
}
If the cell format is:
Empty: cell. CELL_TYPE_BLANK = cell. getCellType ()
Number: cell. CELL_TYPE_NUMERIC = cell. getCellType () | cell. CELL_TYPE_FORMULA = cell. getCellType ()
Character: cell. CELL_TYPE_STRING = cell. getCellType ()
Cell number: cell. getNumericCellValue ()
Cell character: cell. getStringCellValue (). trim ()