Jxls use a template to export an Excel file

Source: Internet
Author: User

/**
* Use a template to export an Excel file
*/
@ Suppresswarnings ({"unchecked", "deprecation "})
@ Override
Public String experexcel (card, httpservletrequest request, httpservletresponse response ){
Try {

Map <object, Object> map = new hashmap <object, Object> ();
Stringbuffer where = new stringbuffer ();
Stringbuffer usesql = new stringbuffer ();
Users user = (users) request. getsession (). getattribute ("user ");
// Determine whether to query the total account
Usesql. append ("from usetbl where useridfk = '" + User. GETID () + "'");

List <usetbl> result = basedao. qryinfo (usesql. tostring (), null); // query database data

Map. Put ("expermessagelist", result );
Map. Put ("experdate", new date ());
String basepath = request. getrealpath ("/");
String temp = user. getname () + "billing information-" + User. GETID () + ". xls ";
Createexcel (basepath + "files/account information .xls", MAP, basepath + "file/down/", temp); // basepath + "files/account information .xls" template path
// Temp = new string (temp. getbytes ("UTF-8"), "iso-8859-1 ");
// Inputstream is = new fileinputstream (new file (basepath + "upload/expermessagedownload/expert info. xls "));
Return download (request, response, basepath + "file/down/" + temp, temp );
} Catch (exception e ){
E. printstacktrace ();
Return "error ";
}
}

 

 

/**
* Use an Excel template to export an Excel file
*
* @ Param templetesrc
* Template path (including name)
* @ Param Params
* Template parameters
* @ Param newexcelpath
* Generate an Excel path
*/
Public static void createexcel (string templetesrc, Map <object, Object> Params,
String newexcelpath, string cre_filename ){
Try {
Xlstransformer transformer = new xlstransformer ();
File file = new file (newexcelpath );
If (! File. exists ()){
File. mkdirs ();
}
Transformer. transformxls (templetesrc, Params, newexcelpath + cre_filename );
} Catch (exception e ){
E. printstacktrace ();
}
}

 

/**
* Download
*
* @ Param request
* @ Param response
* @ Param urlandfile
* File path + file name
* @ Param filename
* File name
* @ Return success and error
* @ Throws exception
*/
Public static string download (httpservletrequest request,
Httpservletresponse response, string urlandfile, string filename)
Throws exception {
String MSG = NULL;
Try {
Response. setcharacterencoding ("UTF-8 ");
Javax. servlet. servletoutputstream ou = response. getoutputstream ();
// File name
// String filename = new
// String (filename. getbytes ("iso8859_1"), "gb2312"). tostring ();

// Path
Java. Io. File file = new java. Io. File (urlandfile );

If (! File. exists ()){
System. Out. println (file. getabsolutepath () + "the file cannot exist! ");
MSG = "unexists ";
Return MSG;
}

// Read the file stream
Java. Io. fileinputstream = new java. Io. fileinputstream (
File );

// Download an object
// Set the response header and download and save the file name

Response. setcontenttype ("application/X-msdownload; charset = UTF-8"); // The download box appears.

Response. setcontentlength (INT) file. Length (); // download statistics file size progress
Response. setheader ("content-disposition", "attachment; filename ="
+ New string (filename. getbytes (), "ISO-8859-1 "));
// Download Box Information
If (fileinputstream! = NULL ){
// Int filelen = fileinputstream. Available ();
// When the file size is too large, the memory cannot be read at a time. It must be cyclically

Byte A [] = new byte [1024];
Int n = 0;
While (n! =-1 ){
N = fileinputstream. Read ();
If (n> 0 ){
Ou. Write (A, 0, N );
}
}
Fileinputstream. Read ();

}
Fileinputstream. Close ();

Ou. Close ();

MSG = "success ";
} Catch (exception e ){
E. printstacktrace ();
MSG = "error ";

}
// After the solution is completed, everything works normally, but the java. Lang. illegalstateexception is thrown mainly because the stream still exists.
Return MSG;
}

######***************************************************************

Name Card No. Current amount Consumption date Capital Trends Consumption Type
<JX: foreach items = "$ {expermessagelist}" Var = "expermessage">      
$ {Expermessage. name} $ {Expermessage. cardfk} $ {Expermessage. xffs} $ {Expermessage. zrzh} $ {Expermessage. statsu} $ {Expermessage. xflx}
</JX: foreach>          

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.