Shh Exporting data to Excel

Source: Internet
Author: User

Recently do a thing to export the data to an Excel table, here is the code

Import Java.io.outputstream;import Java.util.arraylist;import Java.util.list;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.sun.org.apache.regexp.internal.recompile;import entity. Crfhead;import entity.  Crfitem;public class Crfaction {private List dataList = new ArrayList ();p ublic String initinfo () {dataList. Add ("1"); DataList. Add ("2"); DataList. Add ("3"); DataList. Add ("4"); HttpServletResponse response = Servletactioncontext.getresponse (); Setresponseheader (response, "Test.xls"); try { Exportquery (Response.getoutputstream (), dataList); Response.getoutputstream (). Flush (); Response.getoutputstream () . Close ();} catch (Exception e) {///todo:handle exception}//data export buffer empty datalist.clear (); Response_label=1;return "Success";} /** * Set encoding of output stream and output file * @param response * @param filename1 */public void Setresponseheader (HttpServletResponse response,stri ng filename1) {try {response.setcontenttype ("Application/msexcel;charset=utf-8"); Response.setheader (" Content-disposition "," Attachment;filename= "+java.net.urlencoder.encode (filename1," UTF-8 ");//    System.out.println ("a"); Response.AddHeader ("Pargam", "No-cache"); Response.AddHeader ("Cache-control", "No-cache");} catch (Exception e) {e.printstacktrace ();}} public void Exportquery (OutputStream os,list List) {/Create a workbook Hssfworkbook workbook = new Hssfworkbook ();//Create cells, and set the show hssfcellstyle style = Workbook.createcellstyle ();//Create a Tabular format style.setalignment (hssfcellstyle.align_center); /Set format to center//Create a form hssfsheet sheet = workbook.createsheet ("AA");//Add header No. 0 row Hssfrow row = Sheet.createrow ((int) 0) in sheet; /Set table header Hssfcell cell = Row.createcell ((short) 0); Cell.setcellstyle (style); Cell.setcellvalue ("first column"); cell = Row.createcell ((short) 1); Cell.setcellstyle (style); Cell.setCellvalue ("second column"); cell = Row.createcell ((short) 2); Cell.setcellstyle (style); Cell.setcellvalue ("third column"); cell =    Row.createcell ((short) 3); Cell.setcellstyle (style); Cell.setcellvalue ("fourth column");  Row.createcell ((short) 0). Setcellvalue (list.get (0));  Row.createcell ((short) 1). Setcellvalue (List.get (1));  Row.createcell ((short) 2). Setcellvalue (List.get (2)); Row.createcell ((short) 3). Setcellvalue (List.get (3)); Try{workbook.write (OS);    catch (Exception e) {e.printstacktrace ();}         }}

  

Shh Exporting data to Excel

Related Article

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.