(I next is jexcelapi_2_6_12.tar.gz, after decompression will be inside the Jxl.jar copy to web-inf/lib directory below can)
The Java Excel API jar package can be obtained via the following URL: ( recommended )
Http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.6/jexcelapi_2_6_6.zip/download
(all versions included): Http://sourceforge.net/projects/jexcelapi/files/
I. Introduction of JEXCEL
Jexcel is a Java-Excel package that enables you to create an Excel and write or read data from Excel.
The main classes of Jexcel are:
(1) Workbook: Workbooks
(2) Writableworkbook: Writable Workbook
(3) Sheet: Form;
(4) Writablesheet: can write the form;
(5) Label: Cell;
Second, Java writes to Excel
Write Test.xls Data code:
1 ImportJava.io.File; 2 3 ImportJXL. Sheet; 4 ImportJXL. Workbook; 5 ImportJxl.write.Label; 6 ImportJxl.write.WritableSheet; 7 ImportJxl.write.WritableWorkbook; 8 9 Public classCreatetest {Ten One Public Static voidMain (string[] args)throwsexception{ AString str[][] = {{"Xiazdong", "12345"},{"Xzdong", "54321"}}; -File f =NewFile ("Test.xls"); -Writableworkbook Workbook =Workbook.createworkbook (f); theWritablesheet sheet = workbook.createsheet ("Sheet1", 0); -Label Lab =NULL; - for(inti=0;i<str.length;i++){ - for(intj=0;j<str[i].length;j++){ +Lab =NewLabel (J,i,str[i][j]);//Label (COL,ROW,STR); - Sheet.addcell (Lab); + } A } at Workbook.write (); - Workbook.close (); - } - -}
Note: The parameter of the constructor in the label, the first argument is column, the second parameter is row, so if you want to insert the second column of the first row, then (1,0);
Summary: Both Writableworkbook and Writablesheet are used to write Excel files; Read through workbook and sheet execution;
Third, read the Excel data
Read the data in the Test.xls and display the code:
1 ImportJava.io.File; 2 3 ImportJXL. Sheet; 4 ImportJXL. Workbook; 5 6 Public classCreatetest {7 8 Public Static voidMain (string[] args)throwsexception{9Workbook Workbook = Workbook.getworkbook (NewFile ("Test.xls")); TenSheet sheet[] =workbook.getsheets (); OneString Lab =NULL; A for(inta=0;a<sheet.length;a++){ - for(intI=0;i<sheet[a].getrows (); i++){ - for(intJ=0;j<sheet[a].getcolumns (); j + +){ theLab =Sheet[a].getcell (j,i). getcontents (); -System.out.print (lab+ ","); - } - System.out.println (); + } - } + A } at -}
Jexcel Getting Started, Java read and write Excel files