JXL (Java Excel API) using method "1"

Source: Internet
Author: User

//    Building Workbook objects, read-only workbook objects
Create workbook directly from a local document
Create workbook from an input stream

Workbook Workbook = null;
try {
Workbook = Workbook.getworkbook (new File ("D://output.xls"));
} catch (Exception e) {
E.printstacktrace ();
}

//Returns the version number of the API being used, which seems to have little effect.
String apiversion = Workbook.getversion ();
System.out.println (apiversion);

//Get the number of worksheets (Sheet) in the workbook (Workbook)
int sheets = Workbook.getnumberofsheets ();
SYSTEM.OUT.PRINTLN (sheets);

Returns an array of sheet (Sheet) objects in a workbook (Workbook)
sheet[] Sheetstringarr = Workbook.getsheets ();
for (int i=0;i<sheetstringarr.length;i++) {
System.out.println (Sheetstringarr[i].getname ()); return to sheet title

System.out.println (Sheetstringarr[i].getcolumns ());//sheet Total number of columns in the page

System.out.println (sheetstringarr[i].getcolumn (0));//sheet An array of columns of a page

System.out.println (Sheetstringarr[i].getrows ());//total number of rows sheet page
System.out.println (Sheetstringarr[i].getrow (0));//sheet An array of rows of a page
}

Get the first sheet table (term: worksheet), two methods
Sheet Sheet = workbook.getsheet (0); Use subscript to get the first page
Sheet Sheet = Workbook.getsheet ("Testcreateexcel"); Get the first page by name

Returns the first row, the value of the first column (0 (the first represents the column), and 0 (the second represents the row)
Cell Cell00=sheet.getcell (0, 0);
System.out.println (Cell00.gettype ()); Gets the type that represents the class (the type of the returned class)
System.out.println (Cell00.getcontents ());//Gets the type that represents the class (returns a string)

Judging the return value type

if (Celltype.date==cell00.gettype ()) {
System.out.print ("Is Date class" +cell00.gettype ());//date ()
}
if (Celltype.label==cell00.gettype ()) {
System.out.print ("is the Label class" +cell00.gettype ());//lable ()
}
if (Celltype.number==cell00.gettype ()) {
System.out.print ("Is Date class" +cell00.gettype ());//number ()
}

Close Object

Workbook.close ();

===============================================================

Like reading an Excel worksheet, you first use the factory method of the Workbook class to create a writable workbook (Workbook) object, and it is important to note that the Workbook can only be created through the factory method provided by the API. Instead of using the Writableworkbook constructor, the constructor for class Writableworkbook is the protected type.

Building Workbook objects, read-only Workbook objects
Method 1: Create a writable Excel workbook

File Tempfile=new file ("D:/output.xls");
Writableworkbook workbook = workbook. Createworkbook (Tempfile);

Create an Excel worksheet (name, location) that requires two parameters, one that is the name of the worksheet, and the other is a bit of the worksheet in the workbook

Reset
Writablesheet sheet = workbook. Createsheet ("Testcreateexcel", 0);

//1. Adding an object with a font formatting
Writablefont HeaderFont = new Writablefont (Writablefont.arial, Writablefont.bold, False, Underlinestyle.no_  Underline, Jxl.format.Colour.BLUE); Font

2. Format
Writablecellformat Headerformat = new Writablecellformat (HeaderFont); //format, please .

3. Add tags (columns, rows, "contents", fonts)

Jxl.write.Label LABELCF = new Jxl.write.Label (1, 0, "This is a Label Cell", Headerformat);

4. Join the worksheet

Sheet.addcell (labelcf );

Set the width of the column (that column, the width number)

Sheet.setcolumnview (1,20);

//write Exel worksheet
Wwb.write ();

//Close the Excel workbook object
Wwb.close ();

=================================================================================

3. Copy and update Excel workbook

Here's a quick introduction to how to update an existing workbook, mainly the following two steps, the first step is to construct a read-only Excel workbook, and the second step is to create a new writable Excel workbook with the Excel workbook that you created, referring to the following code snippet: ( Complete code See EXCELMODIFYING.JAVA)

Create a read-only Excel Workbook object
Jxl. WorkbookRW= Jxl. Workbook.Getworkbook(New File (sourcefile));

Create a writable Excel workbook object
Jxl.write.WritableWorkbookWWB= Workbook.Createworkbook(New File (targetfile), rw);

Read the first sheet
Jxl.write.WritableSheet ws = Wwb.getsheet (0);//Get first page

Get the first Cell object
Jxl.write.WritableCell WC = Ws.getwritablecell (0, 0);//Get first column, first row

Determine the type of cell and make a corresponding conversion
IfWC. GetType () = = Celltype.label)
{
Label L = (label) WC;//(strong turn) gets the label () object of the cell
L.setstring ("The value has been modified.");//Reset Value
}

//write to Excel object
Wwb.write ();

To close a writable Excel object
Wwb.close ();

Close a read-only Excel object
Rw.close ();

Articles reproduced from http://blog.csdn.net/lcz_ptr/article/details/7687657

JXL (Java Excel API) using method "1"

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.