Writing Excel files using Java--JXL

Source: Internet
Author: User
Tags dateformat

To operate the Excle file, first download the Jxl.jar file, the version I use is 2.6. Download Address: http://www.andykhan.com/jexcelapi/download.html.

Java Excel is an open source project through which Java developers can read the contents of Excel files, create new Excel files, and update existing Excel files. Using this API non-Windows operating systems can also work with Excel data tables through a pure Java application. Because it is written in Java, we can use the JSP, servlet to invoke the API to implement the access to Excel datasheet in the Web application.

1. Create an XLS file

OutputStream os=new FileOutputStream ("C:\\test.xls");

2, the establishment of working papers

Writableworkbook WWB = workbook.createworkbook (OS);

3, if this file already exists, then we can add a sheet in this file in order to separate from the previous data;
Jxl.write.WritableSheet ws = Wwb.createsheet ("Sheet 1", 0);
The previous argument in the Createsheet method is the sheet name, followed by the sheet number to be manipulated.

4. Write Data

(1) The simplest type of character data

Label label = new label (0, 0, "This is a label cell", WCFF);
Ws.addcell (label);
There are three parameters in the label () method
The first is to represent the number of columns,
The second is to represent the number of rows,
The third represents what to write
The fourth option is to enter the style inside the label
Then write the content into the sheet by writing the sheet method Addcell.

Font style:

Writablefont wf = new Writablefont (Writablefont.times, Writablefont.bold, true);
Writablefont () method in the parameter description:
This method is a container that can be put in a lot of properties
The first: Times is the font size, he wrote is 18
The second: bold is to determine if it is italic and to select True as italic
Third: ARIAL
Fourth: Underlinestyle.no_underline underline
Fifth: Jxl.format.Colour.RED font color is red

Writablecellformat WCFF = new Writablecellformat (WF);

(2) Add a number object with formatting
NumberFormat NF = new NumberFormat ("#.##");

(3) Add Number Object

(3.1) Display the number Object data format

NumberFormat NF = new NumberFormat ("#.##");

Writablecellformat WCFN = new Writablecellformat (NF);

Number LABELNF = new number (1,1,3.1415926,WCFN);

Ws.addcell (LABELNF);

Number () method parameter description:

The first two represents the position of the input

The third represents what is entered

(4) Adding a Boolean object

Boolean Labelb = new Boolean (0,2,false);

Ws.addcell (LABELB);

(5) Add a DateTime object

DateTime Labeldt = new DateTime (0,3,new java.util.Date ());

Ws.addcell (Labeldt);

Parameter descriptions for the DateTime () method

The first two positions that represent the input

The third represents the current time entered

(6) Add a DateFormat object with formatting

This displays all information about the current time, including the date and hour of the month.

DateFormat df = new DateFormat ("dd MM yyyy hh:mm:ss");

Writablecellformat wcfdf = new Writablecellformat (DF);

DateTime LABELDTF = new DateTime (1,3,new java.util.Date (), WCFDF);

Ws.addcell (LABELDTF);

(7) Add an object with font color formatting

Writablefont WFC = new Writablefont (writablefont.arial, Writablefont.no_bold, False,underlinestyle.no_underline, colour.red);

Writablecellformat WCFFC = new Writablecellformat (WFC);

Import= "jxl.format.*

Writablefont WFC = new Writablefont (Writablefont.arial,20,writablefont.bold,false,underlinestyle.no_underline, Jxl.format.Colour.GREEN);

(8) Set cell style

Writablecellformat WCFFC = new Writablecellformat (WFC);

Wcffc.setbackground (colour.red);//Set cell color to red

WCFFC = new Label (6,0, "I Love", WCFFC);

The simplest example of a program:

/*

* Created on 2007-12-19

*

* To change the template for this generated file go

* window> Preferences> Java> Code generation> Code and Comments

*/

Package beans;

Import java.io.*;

Import Java.util.Vector;

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.