POI read the Excel 03/07

Source: Internet
Author: User

Java Application can successfully read excel 03 and excel 07, but the JSP servlet cannot read the content in excel 07(.xlsx) for any reason. It can only read the content in excel 03(.xls. The problem persists.
 
/**
* The following jar package is required.
* Poi-3.7-20101029.jar, poi-ooxml-3.7-20101029.jar,
* Xmlbeans-2.3.0.jar, poi-ooxml-schemas-3.7-20101029.jar, dom4j-1.6.1.jar
*/
 
Import java. io .*;
Import org. apache. poi. hssf. usermodel .*;
Import org. apache. poi. openxml4j. exceptions. InvalidFormatException;
Import org. apache. poi. ss. usermodel .*;
 
Public class POIExcelUtils {
Public POIExcelUtils (){
}
/**
* Reading excel, supporting excel 97 ~ 03/excel 07
* @ Param fileName: File Name
*/
Public void read (String fileName ){
Workbook wb = null;
File f = new File (fileName );
FileInputStream is;
Try {
Is = new FileInputStream (f );
Wb = WorkbookFactory. create (is );
ReadWB (wb );
Is. close ();
} Catch (FileNotFoundException e ){
System. out. println (e. getMessage ());
} Catch (IOException e ){
System. out. println (e. getMessage ());
} Catch (InvalidFormatException e ){
System. out. println (e. getMessage ());
}
}
/**
* Reading excel, supporting excel 97 ~ 03/excel 07
* @ Param is: file stream
*/
Public void read (InputStream is ){
Workbook wb;
Try {
Wb = WorkbookFactory. create (is );
ReadWB (wb );
Is. close ();
} Catch (InvalidFormatException e ){
System. out. println (e. getMessage ());
} Catch (IOException e ){
System. out. println (e. getMessage ());
}

}
/**
* Read Workbook
* @ Param wb
* @ Throws Exception
*/
Private void readWB (Workbook wb ){
Try {
// Read sheet0
// For (int k = 0; k <wb. getNumberOfSheets (); k ++ ){
// Sheet
// Sheet sheet = wb. getSheetAt (k );
Sheet sheet = wb. getSheetAt (0 );
ReadRows (sheet); // read by row
// -- Test
/*
System. out. println ("PhysicalNumberOfRows:" + sheet. getPhysicalNumberOfRows ());
System. out. println ("FirstRowNum:" + sheet. getFirstRowNum ());
System. out. println ("LastRowNum:" + sheet. getLastRowNum ());
*/
//}
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
}
/**
* Read each row
* @ Param rows: Valid/non-empty rows
*/
Private void readRows (Sheet sheet ){
Int rows = sheet. getPhysicalNumberOfRows ();
System. out. println (rows );
Int rowIndex = 0; // each row Index
Int notnullRowIndex = 0; // non-empty row Index
While (notnullRowIndex <rows ){
Row row = sheet. getRow (rowIndex );
RowIndex ++;
If (row! = Null ){
ReadCells (row );
NotnullRowIndex ++;
}
}
}
/**
* Read the cells in each row.
* @ Param row: Data in the row
*/
Private void readCells (Row row ){
Int cells = row. getPhysicalNumberOfCells ();
Int cellIndex = 0; // cell index
Int notnullCellIndex = 0; // non-empty cell index
While (notnullCellIndex <cells ){
Cell cell = row. getCell (cellIndex );
CellIndex ++;
If (cell! = Null ){
String value = null;
Switch (cell. getCellType ()){
Case Cell. CELL_TYPE_FORMULA:
Value = "FORMULA value =" + cell. getCellFormula ();
Break;
Case Cell. CELL_TYPE_NUMERIC:
If (HSSFDateUtil. isCellDateFormatted (cell )){
Value = "DATE value =" + cell. getDateCellValue ();
} Else {
Value = "NUMERIC value =" + cell. getNumericCellValue ();
}
Break;
Case Cell. CELL_TYPE_STRING:
Value = "STRING value =" + cell. getStringCellValue ();
Break;
Case Cell. CELL_TYPE_BOOLEAN:
Value = "BOOLEAN value ="
+ Cell. getBooleanCellValue ();
Break;
Default:
}
NotnullCellIndex ++;
System. out. println (value );
}
}
}
}
Author: angus_17

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.