This article refers to:
Http://wenku.baidu.com/view/707f07d95022aaea998f0fd1.html
http://surfingforrest.iteye.com/blog/1709555
http://feitian0127.iteye.com/blog/1152524
Thanks to two-bit.
Packages used:
The code is as follows:
Importjava.util.ArrayList; ImportJava.io.FileInputStream; Importjava.io.IOException; ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook; ImportOrg.apache.poi.ss.usermodel.Cell; ImportOrg.apache.poi.ss.usermodel.Row; ImportOrg.apache.poi.ss.usermodel.Sheet; ImportOrg.apache.poi.ss.usermodel.Workbook; ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; Public classReadexcel { PublicArraylist<arraylist<string>>readexcel (String filename,string path) {ArrayList<ArrayList<String>> Row =NewArraylist<arraylist<string>>(); Try{Workbook Workbook=NULL; Try{WorkBook=NewXssfworkbook (path+ "\ \" +fileName); } Catch(Exception ex) {WorkBook=NewHssfworkbook (NewFileInputStream (path+ "\ \" +fileName)); } for(intNumsheet = 0; Numsheet < Workbook.getnumberofsheets (); numsheet++) {Sheet Sheet=Workbook.getsheetat (Numsheet); if(Sheet = =NULL) { Continue; } //Looping rows Row for(intRowNum = 1; RowNum <= sheet.getlastrownum (); rownum++) {row row=Sheet.getrow (RowNum); if(Row = =NULL) { Continue; } //Cyclic column cellArraylist<string> Arrcell =NewArraylist<string>(); for(intCellnum = 0; Cellnum <= row.getlastcellnum (); cellnum++) {cell cell=Row.getcell (Cellnum); if(Cell = =NULL) { Continue; } arrcell.add (GetValue (cell)); } row.add (Arrcell); } } } Catch(IOException e) {System.out.println ("E:" +e); } returnRow; } PrivateString getValue (cell cell) {if(Cell.getcelltype () = =cell. Cell_type_boolean) {returnstring.valueof (Cell.getbooleancellvalue ()); } Else if(Cell.getcelltype () = =cell. Cell_type_numeric) {returnstring.valueof (Cell.getnumericcellvalue ()); } Else { returnstring.valueof (Cell.getstringcellvalue ()); } } Public Static voidMain (string[] args) {readexcel s=NewReadexcel (); //arraylist<arraylist<string>> row=s.readexcel ("test.xlsx", "D:\\Program Files\\Java"); Arraylist<arraylist<string>> row=s.readexcel ("Test1.xls", "D:\\Program Files\\Java"); System.out.println ("Size:" +row.size ()); for(arraylist<string>Cell:row) { for(String Str:cell) {System.out.println (str); } } } }
Java parsing Excel (2003 and 2007)