Use POI to generate an excel file

Source: Internet
Author: User
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 that the parameters of many methods in the following code 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 );

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.