Manipulate Excel Files--java

Source: Internet
Author: User

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

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.