JAVA------18.excel Export

Source: Internet
Author: User

Background export Excel

1. Importing the JAR Package

Link: Http://pan.baidu.com/s/1c2L70lE Password: nvhh

2. Get the absolute path

2. Source code:

 Packagecom.action;ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.PrintWriter;Importjava.util.ArrayList;Importjava.util.List;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFCellStyle;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.struts2.ServletActionContext;ImportCom.bean.AccountByDay;ImportCom.bean.Qcash;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.util.UrlUtil;/*** Chess Room extract Excel Export *@authorAdministrator **/ Public classQpscashexcelactionextendsactionsupport{PrivateList<qcash>Qcash;  PublicList<qcash>Getqcash () {returnQcash; }     Public voidSetqcash (list<qcash>Qcash) {         This. Qcash =Qcash; } @Override PublicString Execute ()throwsException {//TODO auto-generated Method StubHttpServletResponse response=NULL; Response=Servletactioncontext.getresponse (); HttpServletRequest Request=servletactioncontext.getrequest (); Response.setcontenttype ("Text/html;charset=utf-8"); Response.setcharacterencoding ("UTF-8"); PrintWriter out=Response.getwriter (); //Create Session ObjectHttpSession session=servletactioncontext.getrequest (). GetSession (); if(Session.getattribute ("id") = =NULL) {Out.print ("<script lanaguage= ' JavaScript ' >alert (' Please re-login '); window.location= ' login.jsp ';></script>");            Out.flush ();            Out.close (); return NULL; }        //Card Room application Withdrawal listString url= "Dapail/qps/qpsapplymoneylist.action?"; Jsonobject Salerjson=jsonobject.fromobject (Urlutil.geturl (URL)); String Data=salerjson.get ("Data"). toString (); Jsonobject Jsondata=jsonobject.fromobject (data); Jsonarray Backitemsjson= Jsonarray.fromobject (Jsondata.get ("Qpsmoneyitems"). toString ()); List<Qcash> qc=NewArraylist<qcash>(); if(Backitemsjson.size () >0){                                                //The first step is to create a webbook that corresponds to an Excel fileHssfworkbook WB =NewHssfworkbook (); //In the second step, add a sheet in WebBook that corresponds to the sheet in the Excel fileHssfsheet sheet = wb.createsheet ("Card room application for withdrawal"); //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 shortHssfrow row = Sheet.createrow ((int) 0); //Fourth step, create a cell, and set the value header to center the headerHssfcellstyle style =Wb.createcellstyle (); Style.setalignment (Hssfcellstyle.align_center); //Create a center formatHssfcell Cell= Row.createcell (( Short) 0); Cell.setcellvalue ("Withdrawal ID");              Cell.setcellstyle (style); Cell= Row.createcell (( Short) 1); Cell.setcellvalue ("Chess room Name");              Cell.setcellstyle (style); Cell= Row.createcell (( Short) 2); Cell.setcellvalue ("Chess Room Account");              Cell.setcellstyle (style); Cell= Row.createcell (( Short) 3); Cell.setcellvalue ("Card Name");             Cell.setcellstyle (style); Cell= Row.createcell (( Short) 4); Cell.setcellvalue ("Account Name");             Cell.setcellstyle (style); Cell= Row.createcell (( Short) 5); Cell.setcellvalue ("Bank Card Account");            Cell.setcellstyle (style); Cell= Row.createcell (( Short) 6); Cell.setcellvalue ("Cash withdrawal Amount");            Cell.setcellstyle (style); Cell= Row.createcell (( Short) 7); Cell.setcellvalue ("Withdrawal status");            Cell.setcellstyle (style); Cell= Row.createcell (( Short) 8); Cell.setcellvalue ("Withdrawal Time");                     Cell.setcellstyle (style); //The fifth step, write the Entity data in the actual application of this data from the database,                                                 for(intI=0;i<backitemsjson.size (); i++) {String cash_state=""; Jsonobject Job= Backitemsjson.getjsonobject (i);//iterate through the Jsonarray array and turn each object into a JSON object                if(Job.get ("Cash_state"). ToString (). Equals ("1") ) {cash_state= "In Review"; }Else if(Job.get ("Cash_state"). ToString (). Equals ("2") ) {cash_state= "Audit Success"; }                Else if(Job.get ("Cash_state"). ToString (). Equals ("3") ) {cash_state= "Audit Failed"; } row= Sheet.createrow ((int) i + 1); Row.createcell (( Short) 0). Setcellvalue (Job.get ("cash_id"). toString ());//get the property values in each objectRow.createcell (( Short) 1). Setcellvalue (Job.get ("Qps_name"). toString ());//get the property values in each objectRow.createcell (( Short) 2). Setcellvalue (Job.get ("Qps_account"). toString ());//get the property values in each objectRow.createcell (( Short) 3). Setcellvalue (Job.get ("Card_name"). toString ());//get the property values in each objectRow.createcell (( Short) 4). Setcellvalue (Job.get ("user_name"). toString ());//get the property values in each objectRow.createcell (( Short) 5). Setcellvalue (Job.get ("Card_num"). toString ());//get the property values in each objectRow.createcell (( Short) 6). Setcellvalue (String.Format ("%.3f", Double.valueof (Job.get ("Cash_money"). ToString ())));//get the property values in each objectRow.createcell (( Short) 7). Setcellvalue (Cash_state);//get the property values in each objectRow.createcell (( Short) 8). Setcellvalue (Job.get ("Cash_time"). toString ());//get the property values in each object                }                  //Sixth, save the file to the specified location                Try                  {                      //Project REAL PathString upload = Request.getrealpath ("/"); //Upload=upload.replace ("webapps/back/", "webapps/"); String Path=upload+ "Qpscash";                                        SYSTEM.OUT.PRINTLN (path); File File=NewFile (path); //determine if the saved directory for the uploaded file exists                              if(!file.exists () &&!file.isdirectory ()) {                                 //Create a directoryFile.mkdir (); } fileoutputstream fout=NewFileOutputStream (path+ "/qpscash.xls");                      Wb.write (Fout);                  Fout.close (); }                  Catch(Exception e) {e.printstacktrace (); }                          }        returnSUCCESS; }}

JAVA------18.excel Export

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.