Download the Poi-3.6-20091214.jar as follows:
http://download.csdn.net/detail/evangel_z/3895051
1.jsp
<button type= "button" class= "btn Btn-mini" onclick= "location.href=" <%=basepath%>/bankcard/ Exporteffectthirddata?begintime=${begintime}&endtime=${endtime}&page=1 ' "> Export valid orders </button>
2. Background code Processing:
Controller processing:
@Get ("Exportreturnthirddata")
public void Exportreturnthirdlistdata (Invocation inv, @Param ("BeginTime") String StartTime,
@Param ("Endtime") String Endtime) {
HttpServletResponse response = Inv.getresponse ();
Response.setcontenttype ("Application/xls");
Response.setcontenttype ("Application/x-download");
Response.reset ();
Response.setcontenttype ("bin");
String Header = "Attachment;filename=returnthirddata.xls";
Response.AddHeader ("Content-disposition", header);
String[] Heads = {"Order Date", "Order Number", "Product name", "Commodity Attribute", "channel name", "Payment bank card", "Payment account", "Cost price", "ask", "sell revenue", "Ship Date", "External order audit Date", "Return Date", "Cancellation Date"};
list<fenqigoodsorder> returnorders = Fenqigoodsorderlistservice
. Exportreturnthirdlist (StartTime, endTime);
String path = "";
Orderdetailexportexeclutil.exeportlistdata (heads, returnorders,response);
}
Export to EXECL processing logic:
public static void Exeportlistdata (string[] heads,list<fenqigoodsorder> returnorders,httpservletresponse Response) {
The first step is to create a webbook that corresponds to an Excel file
Hssfworkbook wb = new Hssfworkbook ();
In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel file
Hssfsheet sheet = wb.createsheet ("Third party Return Order Details");
In the third step, add the No. 0 row of the table header to the sheet, noting that the old version of POI has a limit on the number of rows in Excel short
Hssfrow row = Sheet.createrow ((int) 0);
Fourth step, create a cell, and set the value header to center the header
Hssfcellstyle style = Wb.createcellstyle ();
Style.setalignment (Hssfcellstyle.align_center); Create a center format
Hssfcell Cell=null;
for (int h=0;hCell = Row.createcell ((short) h);
Cell.setcellvalue (Heads[h]);
Cell.setcellstyle (style);
}
The fifth step, write the Entity data in the actual application of this data from the database,
for (int i = 0; i < returnorders.size (); i++)
{
row = Sheet.createrow ((int) i + 1);
Fenqigoodsorder Goodsorder = (fenqigoodsorder) returnorders.get (i);
Fourth step, create the cell and set the value
Row.createcell ((short) 0). Setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( Goodsorder.getcreatetime ()));
Row.createcell ((short) 1). Setcellvalue (Goodsorder.getorderno ());
Row.createcell ((short) 2). Setcellvalue (Goodsorder.getgoodname ());
Row.createcell ((short) 3). Setcellvalue (Goodsorder.getgoodtype ());
Row.createcell ((short) 4). Setcellvalue (Goodsorder.getchannelname ());
Row.createcell ((short) 5). Setcellvalue (Goodsorder.getcardno ());
Row.createcell ((short) 6). Setcellvalue (Goodsorder.getaccountno ());
Row.createcell ((short) 7). Setcellvalue (Goodsorder.getpurchaseprice ());//Cost price
Row.createcell ((short) 8). Setcellvalue (Goodsorder.getsellprice ());//Ask Price
Row.createcell ((short) 9). Setcellvalue (Goodsorder.getsellearning ());//Sell income
if (Goodsorder.getdeliverytime ()!=null &&!goodsorder.getdeliverytime (). Equals ("")) {
Row.createcell ((short)). Setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( Goodsorder.getdeliverytime ()));
}
if (Goodsorder.getaudittime ()!=null &&!goodsorder.getaudittime (). Equals ("")) {
Row.createcell (short). Setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( Goodsorder.getaudittime ()));
}
if (Goodsorder.getreturntime ()!=null &&!goodsorder.getreturntime (). Equals ("")) {
Row.createcell (short). Setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( Goodsorder.getreturntime ()));
}
if (Goodsorder.getcanceldate ()!=null &&!goodsorder.getcanceldate (). Equals ("")) {
Row.createcell (short). Setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format ( Goodsorder.getcanceldate ()));
}
}
Sixth, save the file to the specified location
Try
{
Wb.write (Response.getoutputstream ());
}
catch (Exception e)
{
E.printstacktrace ();
}
}
Http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html
Download and export data to EXECL