JSF implementation for Excel generation and download!

Source: Internet
Author: User

Package CN. CCB. Elms. Common;

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. iterator;
Import java. util. List;
Import javax. Faces. Context. facescontext;
Import javax. servlet. servletoutputstream;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfcellstyle;
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;

/**
* <P> Function Description: operations related to File Download </P>
* @ Author gjg
* @ Version 1.0
*/
Public class downloadutil
{
/**
* Generate an Excel file in two-dimensional array format
* @ Param datalist string [] []
*/
Public static void genericexcel (string datalist [] [])
{
String strfilename = "";
Try
{
// Generate an Excel file and save it on the server
Ssconfig SSC = new ssconfig ();
String strzfilename = "query _" + SSC. getpolicimestamp (); // main file name
Strfilename = strzfilename + ". xls"; // full name of the file
Fileoutputstream fileout = new fileoutputstream (strfilename );
Hssfworkbook WB = new hssfworkbook ();
Hssfcellstyle cs = WB. createcellstyle (); // format object
Hssffont fcol = WB. createfont (); // font object, Header
Fcol. setfontheightinpoints (short) 10 );
Fcol. setboldweight (fcol. boldweight_bold );
// Worksheet
Hssfsheet sheet = WB. createsheet (0 + "");
WB. setsheetname (0, strzfilename, (short) 1 );
// Put the first row in the header
Hssfrow ROW = NULL;

Int nrownum = 0;
For (INT nH = 0; NH <datalist. length; NH ++)
{
Row = sheet. createrow (nrownum); // create a row
For (INT nk = 0; NK <datalist [0]. length; NK ++)
{
Hssfcell cell = row. createcell (short) (NK ));
Cell. setencoding (hssfcell. encoding_utf_16 );
Cell. setcellvalue (datalist [NH] [NK]); // write a cell
// Set the header to center in bold
If (nrownum = 0)
{
CS. setfont (fcol );
CS. setalignment (hssfcellstyle. align_center );
Cell. setcellstyle (CS );
}
}
Nrownum ++;
}
WB. Write (fileout );
Fileout. Close ();

}
Catch (exception E)
{
E. printstacktrace ();
}
// Download an object based on the provided file name
Downloadfile (strfilename );
}

/**
* Generate an Excel file in List format
* @ Deprecated
* @ Param colname_zh list
* @ Param data list
*/
Public static void genericexcel2 (list colname_zh, list data)
{
String strfilename = "";
Try
{
If (colname_zh = NULL | DATA = NULL)
{
Throw new exception ("the number of columns in the table header or the number of data columns is empty! ");
}

// Generate an Excel file and save it on the server
Ssconfig SSC = new ssconfig ();
String strzfilename = "query _" + SSC. getpolicimestamp (); // main file name
Strfilename = strzfilename + ". xls"; // full name of the file
Fileoutputstream fileout = new fileoutputstream (strfilename );
Hssfworkbook WB = new hssfworkbook ();
Hssfcellstyle cs = WB. createcellstyle (); // format object
Hssffont fcol = WB. createfont (); // font object, Header
Fcol. setfontheightinpoints (short) 10 );
Fcol. setboldweight (fcol. boldweight_bold );
// Worksheet
Hssfsheet sheet = WB. createsheet (0 + "");
WB. setsheetname (0, strzfilename, (short) 1 );
// Put the first row in the header
Hssfrow ROW = NULL;
Row = sheet. createrow (short) 0 );
Int ny = 0;
For (iterator iter = colname_zh.iterator (); ITER. hasnext ();)
{
Hssfcell cell = row. createcell (short) ny );
Cell. setencoding (hssfcell. encoding_utf_16 );
CS. setfont (fcol );
CS. setalignment (hssfcellstyle. align_center );
Cell. setcellstyle (CS );
Cell. setcellvalue (ITER. Next () + "");
NY ++;
}
// X indicates the starting row of data.
// Y indicates which column the data is filled
Int Nx = 1; // The data starts from the first row.
For (iterator itert = data. iterator (); itert. hasnext ();)
{
NY = 0;
Row = sheet. createrow (short) Nx );
Arraylist Al = (arraylist) itert. Next ();
For (iterator iter2 = al2.iterator (); iter2.hasnext ();)
{
Hssfcell cell = row. createcell (short) ny );
Cell. setencoding (hssfcell. encoding_utf_16 );
// CS. setfont (fcol );
CS. setalignment (hssfcellstyle. align_center );
// Cell. setcellstyle (CS );
Cell. setcellvalue (iter2.next () + "");
NY ++;
}
NX ++;
}
WB. Write (fileout );
Fileout. Close ();
}
Catch (exception E)
{
E. printstacktrace ();
}
// Download an object based on the provided file name
Downloadfile (strfilename );
}

/**
* <P> Function Description: download an object based on the provided file name </P>
* @ Param strfilename string
* @ Return void
*/
Private Static void downloadfile (string strfilename)
{
Try
{
File exportfile = new file (strfilename );
Httpservletresponse = (httpservletresponse)
Facescontext. getcurrentinstance ().
Getexternalcontext (). getresponse ();
Servletoutputstream = httpservletresponse.
Getoutputstream ();
Httpservletresponse. setheader ("content-disposition ",
"Attachment; filename =" + strfilename );
Httpservletresponse. setcontentlength (INT) exportfile. Length ());
Httpservletresponse. setcontenttype ("application/X-download ");
Byte [] B = new byte [1024];
Int I = 0;
Fileinputstream FCM = new java. Io. fileinputstream (exportfile );
While (I = FCM. Read (B)> 0)
{
Servletoutputstream. Write (B, 0, I );
}
}
Catch (ioexception E)
{
E. printstacktrace ();
}
Facescontext. getcurrentinstance (). responsecomplete ();
}

Public static void main (string [] ARGs)
{
// Genericexcel () Usage
String s [] [] = new string [] []
{
{
"Column 1", "column 2", "column 3", "column 4 "},
{
"A", "22", "33", "44 "}
};
Genericexcel (s );

// Genericexcel2 () Usage
// Al indicates the header
Arraylist Al = new arraylist ();
Al. Add ("");
Al. Add ("B ");
Al. Add ("C ");
// Al2 indicates multiple rows of data
Arraylist Al = new arraylist ();
// A3 indicates a row of data
Arraylist _3 = new arraylist ();
Al3.add ("1 ");
Al3.add ("2 ");
Al3.add ("3 ");
Al2.add (_3 );
A3 = new arraylist (); // a new arraylist object must be created here.
Al3.add ("11 ");
Al3.add ("22 ");
Al3.add ("33 ");
Al2.add (_3 );
Genericexcel2 (Al, Al );
}

}

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.