Java Operations Excel First imports Jexcelapi
Jexcelapi is a set of purely Java-developed Excel table operations components, which are not tied to a specific operating system, can operate on different operating systems for Excel files, JXL: http://www.andykhan.com/ jexcelapi/, this article uses the jexcelapi_2_6_12.tar.gz.
There may be a version of JDK and JXL in use, this version is JDK1.6.
Just import the Jxl.jar file in the Jexcelapi development package, the most important are the following classes:
Workbook: Represents a full Excel file
Writableworkbook: Define a blank Excel file to output, but to get this object you need to use the Createworkbook () method of the workbook class to complete
Writablesheet: It means every excell sheet.
Cell: represents each specific cell
Create an Excel file with the following code:
Import Java.io.file;import JXL. Workbook;import jxl.write.label;import jxl.write.writablesheet;import Jxl.write.WritableWorkbook; Public classCreatesimpleexcel { Public Static voidMain (string[] args) throws Exception {String data[][]= { {"even my yes","Oumyye","20 years old" }, { "Oumyye","Oumyye","www.oumyye.com"} };//the information to outputFile OutFile =NewFile ("D:"+ File.separator +"Oumyye.xls"); Writableworkbook Workbook=Workbook.createworkbook (OutFile); Writablesheet sheet= Workbook.createsheet ("Oumyye",0); Label Lab=NULL; for(intx =0; x < data.length; X + +) { for(inty =0; Y < data[x].length; y++) {Lab=NewLabel (y, X, data[x][y]); Sheet.addcell (Lab); }} workbook.write (); Workbook.close (); }}You can also read the information from the XLS:
Import Java.io.file;import JXL. Sheet;import JXL. Workbook; Public classLoadexcel { Public Static voidMain (string[] args) throws Exception {File InFile=NewFile ("D:"+ File.separator +"Oumyye.xls"); Workbook Workbook=Workbook.getworkbook (InFile); Sheet sheet[]=workbook.getsheets (); for(intx =0; x < sheet.length; X + +) { for(inty =0; Y < sheet[x].getrows (); y++) { for(intz =0; Z < Sheet[x].getcolumns (); z++) {String content=Sheet[x].getcell (z, y). getcontents (); System. out. print (Content +"\t\t") ; } System. out. println (); } } }}
Operation Result:
Once you have finished processing your Excel spreadsheet data, be sure to use the close () method to close the previously created object to free up memory space used in reading the data table, which is especially important when reading large amounts of data.
Manipulate Excel Files--java