Apache poi Application

Source: Internet
Author: User
Leave it blank and paste the Code directly.
Package test;

Import java. Io. fileoutputstream;

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;

Public class poi_exceldemo {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
/**
* Author youjianbo
* Date 2008-6-26
* Use Apache poi to generate an Excel file
**/
Try
{
Poi_exceldemo exceldemo = new poi_exceldemo ();
String [] values = {"A", "B", "C", "D", "E", "F", "J "};
// Create a workbook
Hssfworkbook workbook = new hssfworkbook ();
// Create an Excel page
Hssfsheet sheet = Workbook. createsheet ();
Exceldemo. setcolumnwidth (sheet );
// Name sheet and set the encoding.
Workbook. setsheetname (0, "first sheet", hssfworkbook. encoding_utf_16 );
// Create an Excel title

Hssfcellstyle style = exceldemo. createtitlestyle (workbook );
Hssfrow rowtitle = sheet. createrow (0 );
For (INT I = 0; I <3; I ++)
{
Hssfcell celltitle = rowtitle. createcell (short) I );
Celltitle. setcellstyle (style );
Celltitle. setcellvalue ("the" + (I + 1) + "cell ");
}
// Create an Excel row. Here, create two rows and three columns
For (INT I = 1; I <3; I ++)
{
Hssfrow ROW = sheet. createrow (I );
For (Int J = 1; j <4; j ++)
{
Hssfcell cell = row. createcell (short) (J-1 ));
Cell. setcelltype (hssfcell. cell_type_string );
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellstyle (style );
Cell. setcellvalue (Values [I * j]);
}
}
// Sheet. createfreezepane (3, 2); // split the Workspace
// Write the generated Excel File
String Path = exceldemo. getclasspath ();
System. Out. println ("the obtained path is:" + path );
Fileoutputstream filewrite = new fileoutputstream (path + "//" + "exceldemo.xls ");
Workbook. Write (filewrite );
Filewrite. Flush ();
Filewrite. Close ();
System. Out. println ("Excel generated successfully ");
} Catch (exception E)
{
E. printstacktrace ();
System. Out. println (E. getmessage ());
}
}
// Obtain the project directory path
Private string getclasspath ()
{
Return this. getclass (). getclassloader (). getresource (""). getpath ();
}
// Set the cell style
Private hssfcellstyle createtitlestyle (hssfworkbook WB ){
Hssffont boldfont = WB. createfont ();
Boldfont. setfontheight (short) 200 );
Hssfcellstyle style = WB. createcellstyle ();
Style. setfont (boldfont );
Style. setdataformat (hssfdataformat. getbuiltinformat ("###### 0.00 "));
Style. setalignment (hssfcellstyle. align_center );
Return style;
}
// Set the cell width
Private void setcolumnwidth (hssfsheet sheet)
{
// Set the number of columns based on the number of records in your data.
Sheet. setcolumnwidth (short) 0, (short) 3000 );
Sheet. setcolumnwidth (short) 1, (short) 3000 );
Sheet. setcolumnwidth (short) 2, (short) 3000 );
}
}

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.