Use Apache poi to export data to excel

Source: Internet
Author: User
I have nothing to do. I wrote an article about exporting database records to excel in Java, and I will review it again.

Run Apache to download several poi packages and put them in the lib directory.

The following code is directly pasted to read the omitted records from the database. It is assumed that data already exists in my list:

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;

// Only the package to be imported into poi is pasted below.

Public class exceldemo extends dispatchaction
{
/**
* Draw an Excel file
*
* @ Param actionmapping
* @ Param actionform
* @ Param request
* @ Param response
* @ Return
*/
Public actionforward exportsalarybase (actionmapping,
Actionform, httpservletrequest request,
Httpservletresponse response ){
Try {
Messageresources resources (messageresources) request. getattribute (globals. messages_key );
Locale locale = request. getlocale ();
Xxxform form = (xxxform) actionform;
Hssfworkbook workbook = exportexcel ();
If (workbook! = NULL ){
This. printexcel (workbook, response, "exceldemo.xls ");
}
} Catch (exception e ){
Request. setattribute ("errormessage", E. getmessage ());
}
Return NULL;
}
// Export Excel
Private printexcel (hssfworkbook workbook, httpservletresponse response, string filename) Throw pointer ton
{
Outputstream out = response. getoutputstream ();
Response. setheader ("content-disposition", "attachment; filename =" + filename );
Response. setcontenttype ("application/MSExcel; charset = UTF-8 ");
Workbook. Write (out );
Out. Flush ();
Out. Close ();
}
Public hssfworkbook exportexcel () Throw exception
{
Hssfworkbook workbook = NULL;
Try
{
// The data here is the real-time data you want to obtain from the background
List datalist = xxxxdao. findxxxxbyxxxx ();
// Create a workbook instance
Workbook = new hssfworkbook ();
// Create a worksheet instance
Hssfsheet sheet = Workbook. createsheet ("exceldemo ");
// Set the column width
This. setsheetcolumnwidth (sheet );
// Obtain the style
Hssfcellstyle style = This. createtitlestyle (workbook );

//
If (datalist! = NULL & datalist. Size ()> 0)
{
// Create the first line title. Obtain the local information of the Title name from the resource file through resources.
Hssfrow ROW = sheet. createrow (short) 0); // create a new row

This. createcell (row, 0, style, hssfcell. cell_type_string,
Resources. getmessage (locale ,"*****"));
This. createcell (row, 1, style, hssfcell. cell_type_string,
Resources. getmessage (locale ,"*****"));
This. createcell (row, 2, style, hssfcell. cell_type_string,
Resources. getmessage (locale ,"*****"));
This. createcell (row, 3, style, hssfcell. cell_type_string,
Resources. getmessage (locale ,"*****"));
This. createcell (row, 4, style, hssfcell. cell_type_string,
Resources. getmessage (locale ,"*****"));

// Fill data in Excel
For (INT I = 0; I <datalist. Size (); I ++)
{
// Extract the data in datalist. Assume that the model is obtained here, that is, the meaning of a JavaBean.
* ** Model = (*** model) datalist. Get (I );
Hssfrow row1 = sheet. createrow (short) (I + 1); // create a new row
This. createcell (row1, 0, style, hssfcell. cell_type_string,
Model. Get ****());
This. createcell (row1, 1, style, hssfcell. cell_type_string,
Model. Get ****());
This. createcell (row1, 4, style, hssfcell. cell_type_numeric,
Model. Get ****());
This. createcell (row1, 2, style, hssfcell. cell_type_numeric,
Model. Get ****());
This. createcell (row1, 3, style, hssfcell. cell_type_numeric,
Model. Get ****());
} Else {
This. createcell (sheet. createrow (0), 0, style,
Hssfcell. cell_type_string, resources. getmessage (locale,
"Check no information "));
}
}
} Catch (exception E)
{
Request. setattribute ("errormessage", E. getmessage ());
}

}
Private void setsheetcolumnwidth (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 );
Sheet. setcolumnwidth (short) 3, (short) 3000 );
Sheet. setcolumnwidth (short) 4, (short) 5000 );
}
// Set the title style of Excel
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 "));
Return style;
}
// Create an Excel Cell
Private void createcell (hssfrow row, int column, hssfcellstyle style,
Int celltype, object Value ){
Hssfcell cell = row. createcell (short) column );
Cell. setencoding (hssfcell. encoding_utf_16 );
If (style! = NULL ){
Cell. setcellstyle (style );
}
Switch (celltype ){
Case hssfcell. cell_type_blank :{
}
Break;
Case hssfcell. cell_type_string :{
Cell. setcellvalue (value. tostring ());
}
Break;
Case hssfcell. cell_type_numeric :{
Cell. setcelltype (hssfcell. cell_type_numeric );
// Decimalformat format = new decimalformat ("###, ## 0.00 ");
// Cell. setcellvalue (float. parsefloat (value. tostring ()));
Cell. setcellvalue (double. parsedouble (value. tostring ()));
}
Break;
Default:
Break;
}
}
}
Well, I have finished writing it. Here is a summary.
1. First obtain hssfworkbook -----> (through createsheet () -------> Get hssfsheet ---------> (through createrow ())
----------> Get hssfrow ---------> (use createcell ---------> to get hssfcell

2. Fill in values for each cell.

3. Release with response

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.