How to generate an Excel File Using JSP

Source: Internet
Author: User
How to use JSP to generate an Excel file http://code365.com/tech/10/65/Article/5677.Asp
12:01:49 domain name www.code365.com
Browsing statistics Total: 237 | year: 237 | month: 89 | day: 4
Introduction:

Jakarta_poi uses Java to read and write Excel (97-2002) files, which can meet most of the requirements.

Because this tool was used by a project, it took some time to translate poi itself.

A guide is provided with some reductions and modifications. I hope you can get started with this project.

Under poi, there are several self-projects: hssf is used to read and write excel. The following is the home page of hssf.

Http://jakarta.apache.org/poi/hssf/index.html

The following describes the translation based on the following addresses:

Http://jakarta.apache.org/poi/hssf/quick-guide.html

The current version of 1.51 should be a stable version for a long time, but the sample provided by hssf is not based on

1.51, so pay attention to it when using it.

In fact, several sub-projects under poi focus on the development of HDF with different read/write words.

FOP (http://xml.apache.org/fop/index.html) under XML)

PDF files can be output, which is also a good tool

Directory:

Create a workbook

Create a sheet

Create Cells

Created on cells

Set cell format

Note:

The following classes may be used:

Import org. Apache. Poi. hssf. usermodel. hssfcell;

Import org. Apache. Poi. hssf. usermodel. hssfcellstyle;

Import org. Apache. Poi. hssf. usermodel. hssfdataformat;

Import org. Apache. Poi. hssf. usermodel. hssffont;

Import org. Apache. Poi. hssf. usermodel. hssfrow;

Import org. Apache. Poi. hssf. usermodel. hssfsheet;

Import org. Apache. Poi. hssf. usermodel. hssfworkbook;

Import org. Apache. Poi. hssf. util. hssfcolor;

Create a workbook

Hssfworkbook WB = new hssfworkbook ();

// Use the default constructor to create a workbook

Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");

// Specify the file name

WB. Write (fileout );

// Output to file

Fileout. Close ();

Create a sheet

Hssfworkbook WB = new hssfworkbook ();

Hssfsheet sheet1 = WB. createsheet ("new sheet ");

// Create a sheet in the workbook

Hssfsheet sheet2 = WB. createsheet ("second sheet ");

// Create another sheet in the workbook

Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");

WB. Write (fileout );

Fileout. Close ();

Create Cells

Hssfworkbook WB = new hssfworkbook ();

Hssfsheet sheet = WB. createsheet ("new sheet ");

// Note the followingCodeThe parameters of many methods are short rather than int, so a type conversion is required.

Hssfrow ROW = sheet. createrow (short) 0 );

// Create a row in sheet

Hssfcell cell = row. createcell (short) 0 );

// Create a cell in the row

Cell. setcellvalue (1 );

// Set the cell value

// The value type parameters include multiple double, String, Boolean,

Row. createcell (short) 1). setcellvalue (1.2 );

Row. createcell (short) 2). setcellvalue ("this is a string ");

Row. createcell (short) 3). setcellvalue (true );

// Write the output to a file

Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");

WB. Write (fileout );

Fileout. Close ();

Created on cells

Hssfworkbook WB = new hssfworkbook ();

Hssfsheet sheet = WB. createsheet ("new sheet ");

Hssfrow ROW = sheet. createrow (short) 0 );

Hssfcell cell = row. createcell (short) 0 );

// Set the value to date

Cell. setcellvalue (new date ());

Hssfcellstyle cellstyle = WB. createcellstyle ();

// Specify the date display format

Cellstyle. setdataformat (hssfdataformat. getformat ("m/D/yy H: mm "));

Cell = row. createcell (short) 1 );

Cell. setcellvalue (new date ());

// Set the display format of the cell date

Cell. setcellstyle (cellstyle );

Fileoutputstream fileout = new fileoutputstream ("workbook.xls ");

WB. Write (fileout );

Fileout. Close ();

Set cell format

Cell format settings include cell alignment and content font settings,

Cell background color, because there are many forms, only some examples are given. The following example shows

Poi1.5 may change.

..........

// Aqua background

Hssfcellstyle style = WB. createcellstyle ();

// Create a style

Style. setfillbackgroundcolor (hssfcellstyle. Aqua );

// Set the background color fill for this style

Style. setfillpattern (hssfcellstyle. big_spots );

// Fill type of the style.

// There are multiple styles such:

// Hssfcellstyle. big_spots

// Hssfcellstyle. fine_dots

// Hssfcellstyle. sparse_dots, etc.

Style. setalignment (hssfcellstyle. align_center );

// Align

Style. setfillbackgroundcolor (hssfcolor. Green. Index );

// Set the background color of the Unit.

Style. setfillforegroundcolor (hssfcolor. Red. Index );

// Set the cell display color

Hssfcell cell = row. createcell (short) 1 );

Cell. setcellvalue ("X ");

Cell. setcellstyle (style );

Related Article

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.