Java Learning Path: Java operations Excel export download

Source: Internet
Author: User

Page Place a button to click Export Event

action= "#{eveluatesubject.dosaveexcel}"
styleclass= "CSSBUTTONBG" >

</H:COMMANDLINK>
         &NBSP;&NBSP
Export Method
/**
  * Export Excel
  * 
  * @param request
  * @return
  */
 public String dosaveexcel () {
  httpservletrequest request = (httpservletrequest) Facescontext
    .getcurrentinstance (). Getexternalcontext (). Getrequest ();
  dooutof (request);

return null;
}

Public String Dooutof (HttpServletRequest request) {

  //filename and path
  string Realpath = Request.getrealpath (" ");
  string filename = "exported file name. xls";
  file file = new file (Realpath + "//xxfzpj//temppath//");//location where the exported file is stored
  if (!file.exists () ) {
   file.mkdirs ();
  }
  realpath = Realpath + "//xxfzpj//temppath//" + fileName;

  //Create a workbook and write the table header
  try {
    Writableworkbook WWB = Workbook.createworkbook (new File (Realpath));
   writablesheet ws = Wwb.createsheet ("Sheet1", 0);//Create Workbook

   //Write table header
   jxl.write.label Label1 = new Jxl.write.Label (0, 0, "column 1");
   jxl.write.label label2 = new Jxl.write.Label (1, 0, "column 2");
   jxl.write.label label3 = new Jxl.write.Label (2, 0, "column 3");
   jxl.write.label label4 = new Jxl.write.Label (3, 0, "column 4");
   jxl.write.label label5 = new Jxl.write.Label (4, 0, "column 5");

Ws.addcell (Label1);//Insert Workbook
Ws.addcell (Label2);
Ws.addcell (LABEL3);
Ws.addcell (LABEL4);
Ws.addcell (LABEL5);

Write information
list<map> listmsg = read-out content in database (running SQL);

for (int i = 0; i < listmsg.size (); i++) {
Label1 = new Jxl.write.Label (0, i + 1, listmsg.get (i). Get (
"subject_id"). ToString (). Trim ());//Set up the first column
Label2 = new Jxl.write.Label (1, i + 1, listmsg.get (i). Get (
"Subject_name"). ToString (). Trim ());//Create second column
if (Listmsg.get (i). Get ("object_name")! = NULL
&& ""! = Listmsg.get (i). Get ("object_name")) {
Label3 = new Jxl.write.Label (2, i + 1, listmsg.get (i). Get (
"object_name"). ToString (). Trim ());//Create a third column
} else {
Label3 = new Jxl.write.Label (2, i + 1, "Evaluation object name not defined!") ");
}
Label4 = new Jxl.write.Label (3, i + 1, listmsg.get (i). Get (
"Mode_name"). ToString (). Trim ());//Build Fourth column
Label5 = new Jxl.write.Label (4, i + 1, listmsg.get (i). Get (
"Type_name"). ToString (). Trim ());//Build Fifth Column
Ws.addcell (Label1);//Insert Workbook
Ws.addcell (Label2);
Ws.addcell (LABEL3);
Ws.addcell (LABEL4);
Ws.addcell (LABEL5);
}

Writing Exel Worksheets
Wwb.write ();
Close the Excel Workbook object
Wwb.close ();

Download
Facescontext CTX = Facescontext.getcurrentinstance ();

   string contentType =" Application/x-download ";
   httpservletresponse response = (httpservletresponse) ctx
     . Getexternalcontext (). GetResponse ();

   response.setcontenttype (ContentType);
   response.setheader ("Content-disposition", "Attachment;filename="
      + New String (Filename.getbytes ("gb2312"), "iso8859-1"));
   httpservletrequest request1 = (httpservletrequest) facescontext
      .getcurrentinstance (). Getexternalcontext (). Getrequest ();

   servletoutputstream out = Response.getoutputstream ();

   byte[" bytes = new BYTE[0XFFFF];
   inputstream is = new FileInputStream (new File (Realpath));
   int b = 0;
   while ((b = is.read (bytes, 0, 0xffff)) > 0) {
    out.write (bytes, 0, B) ;
   }
   is.close ();
   out.flush ();
   ctx.responsecomplete ();

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}//here to establish the path
return null;
}

Java Learning Path: Java operations Excel export download

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.