Use jxls to generate and download an Excel file

Source: Internet
Author: User
Jxl. jar jxls-core-0.9.9.jarjxls-reader-0.9.9.jarpoi-3.5-FINAL.jar (must be a version later than 3.5) other jar according to the prompts can go to the http://jarvana.com/jarvana/ to find // generate EXCEL input template file to generate the content of the file to return the complete path of the generated file public static string doexcel (string from, map beans, string to) {servletcontext SC = (servletcontext) actioncontext. getcontext (). get (servletactioncontext. servlet_context); string Path = SC. getrealpath ("/model"); xlstransformer transformer = N EW xlstransformer (); string sfrom = path + "\" + from; // template file string sto = path + "\" +; // try {transformer. transformxls (sfrom, beans, STO);} catch (parsepropertyexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} return sto;} // download public static void dodownload (string path, string name, httpservl Etresponse response) {try {response. reset (); response. setheader ("content-disposition", "attachment; success = true; filename =" + urlencoder. encode (name, "UTF-8"); bufferedinputstream Bis = NULL; bufferedoutputstream Bos = NULL; outputstream Fos = NULL; inputstream FD = NULL; file uploadfile = new file (PATH ); FS = new fileinputstream (uploadfile); Bis = new bufferedinputstream (FS); Fos = response. getoutputstr EAM (); Bos = new bufferedoutputstream (FOS); // the pop-up Download Dialog Box int bytesread = 0; byte [] buffer = new byte [8192]; while (bytesread = bis. read (buffer, 0, 8192 ))! =-1) {Bos. write (buffer, 0, bytesread);} Bos. flush (); FCM. close (); bis. close (); FOS. close (); Bos. close ();} catch (exception e) {e. printstacktrace () ;}} call: List resultlist = new arraylist (); For (INT I = 0; I <list. size (); I ++ ){...... Inspectionstisfaction Vo = new inspectionstisfaction (); vo. setxjrq (systemutil. gettimestr2str (string. valueof (map. get ("apply_date"); vo. setxjsl (lxjsl); vo. setfcmy (lfcmy); vo. setmy (lmy); vo. setyb (lyb); vo. setbmy (lbmy); vo. setfcbmy (lfcbmy); resultlist. add (VO);} // generate excelmap beans = new hashmap (); beans. put ("resultlist", resultlist); string Path = systemutil. doexcel ("Inspection merchant satisfaction Report Template .xls", beans, "Inspection merchant satisfaction report .xls"); systemutil. dodownload (path, "Inspection merchant satisfaction report .xls", this. response); return NULL; Template style: number of inspection (items) Overall Satisfaction satisfaction general dissatisfaction total <JX: foreach items = "$ {resultlist}" Var = "L" >$ {L. xjrq }$ {L. xjsl }$ {L. fcmy }$ {L. my} $ {L. yb} $ {L. bmy} $ {L. fcbmy} $ [B # + C # + D #] </JX: foreach> total number of months (count) $ [sum (B4)] $ [sum (D4)] $ [sum (E4)] $ [sum (F4)] $ [sum (G4)] $ [sum (h4)] $ [sum (I4)]

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.