Java reads XLS and xlsx data as data-driven

Source: Internet
Author: User

Java read excle code can be used directly: for XLS and xlsx

 PackageDataprovider;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.DateUtil;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.xssf.usermodel.XSSFSheet;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; Public classDp_demo { Public StaticObject[][] Gettestdata (String filePath)throwsFileNotFoundException, IOException {File excelfile=NewFile (FilePath); /** Determine the type of the given file;         1. If the ask price of the XLS type, create Xssfworkbook;         * 2. If the file type is xlsx, create hssfworkbook; */String xls= Filepath.substring (Filepath.indexof ('. '))); System.out.println ("The suffix of the incoming file is:" + xls + ";"); if(Xls.equals (". xls") {Hssfworkbook XLSWB=NewHssfworkbook (NewFileInputStream (excelfile)); Hssfsheet sheet= Xlswb.getsheetat (0); intRowCount = Sheet.getlastrownum ()-Sheet.getfirstrownum (); List<Object[]> list =NewArraylist<object[]>(); //System.out.println ("---------the sheet total:" + rowcount + ";");row Row;            Cell cell;  for(inti = 0; I < ROWCOUNT + 1; i++) {row=Sheet.getrow (i); if(Row.getcell (i) = =NULL){                    Continue ; }Else{} object[] obj=NewObject[row.getlastcellnum ()]; /** SYSTEM.OUT.PRINTLN ("The current line is:" + (Row.getrownum () + 1) + * "; when the first cell ahead is:" + Row.getfi Rstcellnum () + ";                 The last cell before the current is: "* + row.getlastcellnum () +"; "); */                //System.out.println ("The length of the obj array is:" + obj.length + ";");                 for(intj = 0; J < Row.getlastcellnum (); J + +) {cell=Row.getcell (j); Switch(Cell.getcelltype ()) { CaseCell.cell_type_string:obj[j]=Cell.getrichstringcellvalue (). getString (); /** System.out.print (Cell.getrichstringcellvalue (). * GetString ());                         System.out.print ("|"); */  Break;  CaseCell.cell_type_numeric:if(dateutil.iscelldateformatted (cell)) {Obj[j]=Cell.getdatecellvalue (); //System.out.print (string.valueof (Cell.getdatecellvalue ()));}Else{Obj[j]=Cell.getnumericcellvalue (); //System.out.print (Cell.getnumericcellvalue ());                        }                        //System.out.print ("|");                         Break;  CaseCell.cell_type_boolean:obj[j]=Cell.getbooleancellvalue (); /** System.out.print (Cell.getbooleancellvalue ());                         * System.out.print ("|"); */  Break; default:                    }                }                List.add (obj); //System.out.println ();            }            //System.out.println ("list.size () = = =" + list.size ());Object[][] Object =Newobject[list.size () [];  for(inti = 0; i < object.length; i++) {Object[i]=List.get (i); }            returnobject; } Else if(Xls.equals (". xlsx")) {Xssfworkbook wbxlsx=NewXssfworkbook (NewFileInputStream (excelfile)); Xssfsheet sheet= Wbxlsx.getsheetat (0); intRowCount = Sheet.getlastrownum ()-Sheet.getfirstrownum (); List<Object[]> list =NewArraylist<object[]>(); //System.out.println ("---------the sheet total:" + rowcount + ";");row Row;            Cell cell;  for(inti = 0; I < ROWCOUNT + 1; i++) {row=Sheet.getrow (i); /** SYSTEM.OUT.PRINTLN ("The current line is:" + (Row.getrownum () + 1) + * "; when the first cell ahead is:" + Row.getfi Rstcellnum () + ";                 The last cell before the current is: "* + row.getlastcellnum () +"; "); */object[] obj=NewObject[row.getlastcellnum ()]; //System.out.println ("The length of the obj array is:" + obj.length + ";");                 for(intj = 0; J < Row.getlastcellnum (); J + +) {cell=Row.getcell (j); Switch(Cell.getcelltype ()) { CaseCell.cell_type_string:obj[j]=Cell.getrichstringcellvalue (). getString (); /** System.out.print (Cell.getrichstringcellvalue (). * GetString ());                         System.out.print ("|"); */                         Break;  CaseCell.cell_type_numeric:if(dateutil.iscelldateformatted (cell)) {Obj[j]=Cell.getdatecellvalue (); //System.out.print (string.valueof (Cell.getdatecellvalue ()));}Else{Obj[j]=Cell.getnumericcellvalue (); //System.out.print (Cell.getnumericcellvalue ());                        }                        //System.out.print ("|");                         Break;  CaseCell.cell_type_boolean:obj[j]=Cell.getbooleancellvalue (); /** System.out.print (Cell.getbooleancellvalue ());                         * System.out.print ("|"); */                         Break; default:                    }                }                List.add (obj);            System.out.println (); }            //System.out.println ("list.size () = = =" + list.size ());Object[][] Object =Newobject[list.size () [];  for(inti = 0; i < object.length; i++) {Object[i]=List.get (i); }            returnobject; } Else{System.out.println ("The specified file is not a excle file! "); }        return NULL; }     Public Static voidMain (string[] args)throwsFileNotFoundException, IOException {//String Filepathxls = "D:\\2003table15.xls";String filepathxlsx = "D:\\2007text.xlsx"; //Object [] Objxls =dp_demo.gettestdata (filepathxls);object[][] Objxlsx =Dp_demo.gettestdata (FILEPATHXLSX); /** for (int i = 0; i < objxlsx.length; i++) {* SYSTEM.OUT.PRINTLN (objxlsx[i]);} */object[] Obj1= Objxlsx[0];  for(inti = 0; i < obj1.length; i++) {System.out.print ("[" + Obj1[i] + "]"); }    }}

Java reads XLS and xlsx data as data-driven

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.