Struts2+extjs+poi exporting Excel

Source: Internet
Author: User

First build Excel

Import java.io.IOException;

Import Java.io.OutputStream;

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.HSSFRow;

Import Org.apache.poi.hssf.usermodel.HSSFSheet;

Import Org.apache.poi.hssf.usermodel.HSSFWorkbook;

Import Org.apache.struts2.ServletActionContext;

Import Com.acbee.model.User;


Classes that generate Excel and output

public class Createexcel {

@SuppressWarnings ("deprecation")

Public String Create ()

{

String filename= "student";

//First step, create a webbook, corresponding to an Excel file

Hssfworkbook wb = new Hssfworkbook ();


///second step, add a sheet in WebBook, corresponding to the sheet in the Excel file

Hssfsheet sheet = wb.createsheet ("Student form One");


///step three, add the header line No. 0 to the sheet, and note that the older POI has a limit on the number of rows in Excel short

Hssfrow row = Sheet.createrow ((int) 0);


//Fourth step, create the cell and set the value header to center the table header

Hssfcellstyle style = Wb.createcellstyle ();

Style.setalignment (Hssfcellstyle.align_center); Create a center format

Hssfcell cell = Row.createcell ((short) 0);

Cell.setcellvalue ("School Number");

Cell.setcellstyle (style);

Cell = Row.createcell ((short) 1);

Cell.setcellvalue ("name");

Cell.setcellstyle (style);

Cell = Row.createcell ((short) 2);

Cell.setcellvalue ("Age");

Cell.setcellstyle (style);

Cell = Row.createcell ((short) 3);

Cell.setcellvalue ("Birthday");

Cell.setcellstyle (style);


//Fifth, write the data from the database in the actual application of the physical data,

List List = Createsimpleexceltodisk.getstudent ();

for (int i = 0; i < list.size (); i++)

//        {

row = Sheet.createrow ((int) i + 1);

User user = (user) list.get (i);

Row.createcell ((short) 0). Setcellvalue ((double) user.getuser_pk ());

Row.createcell ((short) 1). Setcellvalue (User.getuser_name ());

Row.createcell ((short) 2). Setcellvalue (User.getuser_number ());

Cell = Row.createcell ((short) 3);

Cell.setcellvalue (New SimpleDateFormat ("Yyyy-mm-dd"). Format (User.getuser_birthday ()));

//        }


for (int i = 0; i < 3; i++)

{

row = Sheet.createrow ((int) i + 1);

User user = (user) list.get (i);

Row.createcell ((short) 0). Setcellvalue (1);

Row.createcell ((short) 1). Setcellvalue (2);

Row.createcell ((short) 2). Setcellvalue (3);

Row.createcell ((short) 3). Setcellvalue (3);

}


//Sixth step, output the file via httpservletresponse

          /*

* The following can not be written, direct copy

*/

HttpServletResponse response = null;//Create a HttpServletResponse object

OutputStream out = null;//Create an output stream object

try {

System.out.println ("io");

Response = Servletactioncontext.getresponse ();//Initialize HttpServletResponse object

out = Response.getoutputstream ();//

Response.setheader ("Content-disposition", "attachment; Filename= "+ filename+". xls ");

FileName is the name of the downloaded XLS

Response.setcontenttype ("Application/msexcel;charset=utf-8");//set type

Response.setheader ("Pragma", "No-cache");//Set Header

Response.setheader ("Cache-control", "No-cache");//Set Header

Response.setdateheader ("Expires", 0);//Set Date header

Wb.write (out);

Out.flush ();

Wb.write (out);

} catch (IOException e) {

E.printstacktrace ();

} finally {

if (out! = null) {

System.out.println ("Out is close");

Out.close ();

}

}

return null;

}

}


Call the method that generates excel in action

Createexcel excel=new createexcel ();

Public String Export ()

{

SYSTEM.OUT.PRINTLN ("Export");

Url=excel. Create ();

return SUCCESS;

System.out.println (URL);

Return null;// here must be null otherwise error

}


The calling code in ExtJS

Event for Export button

var export_excel=function (BTN)

{

Ext.ajax omitted

Call Export.action Export Excel

//You must do this here, or you won't be able to pop up the box that saved Excel

Window.location= "Export.action";

}



Configuration in the Struts.xml

<package name= "ExtJS" extends= "Json-default" namespace= "/" >

<action name= "Export" class= "useraction" method= "Export" >

<result type= "JSON" >

</result>

</action>

</package>

Or

<package name= "Default" namespace= "/" extends= "Struts-default" >

<action name= "Export" class= "useraction" method= "Export" >

<result name= "Success" type= "Stream" >

</result>

</action>

</package>

It is verified that both of the above methods can be used, mainly when you call action in ExtJS to note the wording, you can successfully export Excel


Struts2+extjs+poi exporting Excel

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.