Jxl-Java generates an Excel table

Source: Internet
Author: User
Tags add numbers

Well, I learned how to operate jxl.

Step 1:

String filepath = "d :\\ test.xls ";

Writableworkbook WWB;
Outputstream OS = new fileoutputstream (new file (filepath ));
WWB = Workbook. createworkbook (OS );

// Use the file output stream to generate an Excel file for xls

 

 

Step 2: Create a sheet table

Writablesheet sheet = WWB. createsheet ("product list", 0 );

 

Step 3: Use the label that comes with jxl to add content to a specified location

Label Label = new label (INT column, int row, string content, jxl. format. cellformat format );

Four parameters:

Column

Row

Content

Format (optional) cellformat is an interface with multiple formatting subclasses.

Step 4: Use jxl. Write. Number to add numbers to a specified position

1. Define a formatting template first

Jxl. Write. numberformat NF = new jxl. Write. numberformat ("#.##");

2. writablecellformat is a subclass of cellformat and accepts a template parameter.

Jxl. Write. writablecellformat WCF = new jxl. Write. writablecellformat (NF );

3. Add a number to the specified position and specify the format template
Jxl. Write. Number NB = new jxl. Write. Number (2, 1, 2.45, WCF );

Step 5: add the corresponding worksheet after the location is specified:

Sheet. addcell (writablecell)

Step 6: Write Data and close the output stream

WWB. Write ();

WWB. clost ();

 

Supplement: Write of Boolean value and date format:

Write method of Boolean value:

Jxl. Write. boolean flag = new jxl. Write. boolean (6, 1, true );
Sheet. addcell (FLAG );

Write method in date format:

/**
* Defines the public format of the display date.
* Example: yyyy-mm-dd hh: mm
*/
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
String newdate = SDF. Format (New java. util. Date ());
// Fill in the generated date
Label = new label (4,1, newdate );
Sheet. addcell (Label );

 

 

 

 

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.