JAVA excel Import

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.