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