Detailed general steps for exporting Excel tables using JXL in Java

Source: Internet
Author: User

This method typically receives two parameters, response, and a list of table contents to be Exported.

In general, we will query the database data to show on the page, according to user needs, may need to export the page Data.

At this point, as long as the data displayed before the query into the session to back up a copy, when the export method is called, from the session to get,

If the direct export for the background, directly query the database after the results are passed, of course, you can also query in the export Excel method.

Query method:

Get query results in session
Object resultlist = request.getattribute ("listItems");//the Query result set presented to the foreground
Request.getsession (). SetAttribute ("resultlist", resultlist);

To export an Excel method:

Get the query result set from the session
list<ttmoasconorecordinfo> resultlist = (list<ttmoasconorecordinfo>) request.getsession (). getAttribute ("resultlist");

Call the export Excel method, Pass in response, and Resultlist
Xsdwglservice.downloadexcel (response, resultlist);

Downloadexcel as Follows:

@Override public voidDownloadexcel (httpservletresponse response, list<ttmoasconorecordinfo>List) {        //Create a worksheetWritableworkbook book =NULL;        Response.reset (); Response.setcharacterencoding ("UTF-8");//Set character sets//Create a WorkflowOutputStream OS =NULL; Try {            //Set Popup dialog boxResponse.setcontenttype ("application/dowload"); Response.setcharacterencoding ("UTF-8"); //set the title of a worksheetResponse.setheader ("content-disposition", "attachment; Filename=norecord_social_credit_code.xls ");//set the generated file nameOS =Response.getoutputstream (); //Initializing a worksheetBook =Workbook.createworkbook (os); } Catch(ioexception e1) {logger.error ("an IO exception occurred in export excel", e1); Throw NewServiceexception ("export failed", e1); }        Try {            //The following is the Excel table content//int ncount = list.size ();Writablesheet sheet = Book.createsheet ("non-union and inconsistent name Preservation results", 0); //build Name sheet, parameter 0 indicates this is the first page//int NI = 1; //table field nameSheet.addcell (NewJxl.write.Label (0, 0, "serial Number"))); Sheet.addcell (NewJxl.write.Label (1, 0, "name (query)")); Sheet.addcell (NewJxl.write.Label (2, 0, "business status")); Sheet.addcell (NewJxl.write.Label (3, 0, "name (company name)")); Sheet.addcell (NewJxl.write.Label (4, 0, "unified Social Credit Code"))); Sheet.addcell (NewJxl.write.Label (5, 0, "application date")); //Append data to             for(inti = 1; I < list.size () + 1; i++) {sheet.addcell (NewJxl.write.Label (0, i, string.valueof (i)));//serial number starting from 1Sheet.addcell (NewJxl.write.Label (1, i, list.get (i-1). Getentname ())); Sheet.addcell (NewJxl.write.Label (2, i, list.get (i-1). getentstatus ())); Sheet.addcell (NewJxl.write.Label (3, i, list.get (i-1). GetUserName ())); Sheet.addcell (NewJxl.write.Label (4, i, list.get (i-1). Getappcertificatenum ())); //Set Date formatSimpleDateFormat SF =NewSimpleDateFormat ("yyyy-mm-dd"); Date appdate= List.get (i-1). getappdate (); String Appdatestr=Sf.format (appdate); Sheet.addcell (NewJxl.write.Label (5, i, appdatestr));//Application Date} book.write ();        Book.close (); } Catch(Exception e) {logger.error ("export Excel Exception occurred", e); Throw NewServiceexception ("export failed", e); } finally {            if(NULL!=OS) {                Try{os.close (); } Catch(ioexception e) {logger.error ("off-stream Exception occurred", e);                E.printstacktrace (); }            }        }    }

This will allow you to export the Excel Table.

You need to set multiple sheet/merged cells, etc., according to the corresponding API Settings.

Detailed general steps for exporting Excel tables using JXL in Java

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.