Using the POI jar package, compatible with EXCEL2003 and 2007+ versions of Excel
Required JAR Packages:
Poi-3.8.jar
Poi-ooxml.jar
Poi-ooxml-schemas.jar
Xmlbeans.jar
The code is as follows:
Public classExcelreader {PrivateString FilePath; PrivateString SheetName; PrivateWorkbook Workbook; PrivateSheet Sheet; PrivateList<string>columnheaderlist; PrivateList<list<string>>ListData; PrivateList<map<string,string>>MapData; Private BooleanFlag; PublicExcelreader (String filePath, String sheetname) { This. FilePath =FilePath; This. SheetName =SheetName; This. Flag =false; This. Load (); } Private voidload () {FileInputStream instream=NULL; Try{instream=NewFileInputStream (NewFile (FilePath)); WorkBook=workbookfactory.create (instream); Sheet=Workbook.getsheet (SheetName); } Catch(Exception e) {e.printstacktrace (); }finally{ Try { if(instream!=NULL) {instream.close (); } } Catch(IOException e) {e.printstacktrace (); } } } Privatestring Getcellvalue (cell cell) {string Cellvalue= ""; Dataformatter Formatter=NewDataformatter (); if(Cell! =NULL) { Switch(Cell.getcelltype ()) { CaseCell.cell_type_numeric:if(dateutil.iscelldateformatted (cell)) {Cellvalue=Formatter.formatcellvalue (cell); } Else { DoubleValue =Cell.getnumericcellvalue (); intIntvalue = (int) value; Cellvalue= Value-intvalue = = 0?string.valueof (intvalue): string.valueof (value); } Break; CaseCell.CELL_TYPE_STRING:cellValue=Cell.getstringcellvalue (); Break; CaseCell.CELL_TYPE_BOOLEAN:cellValue=string.valueof (Cell.getbooleancellvalue ()); Break; CaseCell.CELL_TYPE_FORMULA:cellValue=string.valueof (Cell.getcellformula ()); Break; CaseCell.CELL_TYPE_BLANK:cellValue= ""; Break; CaseCell.CELL_TYPE_ERROR:cellValue= ""; Break; default: Cellvalue=cell.tostring (). Trim (); Break; } } returnCellvalue.trim (); } Private voidGetsheetdata () {ListData=NewArraylist<list<string>>(); MapData=NewArraylist<map<string, string>>(); Columnheaderlist=NewArraylist<string>(); intNumofrows = Sheet.getlastrownum () + 1; for(inti = 0; i < numofrows; i++) {row row=Sheet.getrow (i); Map<string, string> map =NewHashmap<string, string>(); List<String> list =NewArraylist<string>(); if(Row! =NULL) { for(intj = 0; J < Row.getlastcellnum (); J + +) {cell cell=Row.getcell (j); if(i = = 0) {Columnheaderlist.add (Getcellvalue (cell)); } Else{Map.put (Columnheaderlist.get (j), This. Getcellvalue (cell)); } list.add ( This. Getcellvalue (cell)); } } if(I > 0) {mapdata.add (map); } listdata.add (list); } Flag=true; } PublicString Getcelldata (intRowintCol) { if(Row<=0 | | col<=0){ return NULL; } if(!flag) { This. Getsheetdata (); } if(Listdata.size () >=row && listdata.get (row-1). Size () >=Col) { returnListdata.get (row-1). Get (Col-1); }Else{ return NULL; } } PublicString Getcelldata (introw, String headername) { if(row<=0){ return NULL; } if(!flag) { This. Getsheetdata (); } if(Mapdata.size () >=row && mapdata.get (row-1). ContainsKey (Headername)) {returnMapdata.get (row-1). Get (Headername); }Else{ return NULL; } } Public Static voidMain (string[] args) {Excelreader eh=NewExcelreader ("E:\\workspace\\test.xls", "Sheet1"); System.out.println (Eh.getcelldata (1, 1)); System.out.println (Eh.getcelldata (1, "Test1")); }}
Java parsing Excel