Apache poi Application
Last Update:2018-12-03
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 );
}
}