Java Report download code excel

Source: Internet
Author: User

    /*** Summary Report data download **/        PrivateModelandview Exportsummarydatadown (httpservletrequest request, httpservletresponse response, List<InterfaceCost> interfacecostlistnew)throwsException {//Save to diskString time = qdatetime.datetostring (NewDate (), "Yyyy-mm-dd HH:mi:ss"); time= Time.replaceall ("-", "" "); time=time.replace (":", "" "); time=time.replace ("", "" "); time= Time.substring (2); String file_name= time + ". xls"; String Uploaddir= Request.getrealpath ("/resources") + "\\Interfaceparameter\\"; OutputStream out=NULL; Try{File Dirpath=NewFile (Uploaddir); if(!dirpath.exists ())              {Dirpath.mkdirs (); } out=NewFileOutputStream (uploaddir+file_name); //Set the first row (format of the table header)Jxl.write.WritableFont WFC =NewJxl.write.WritableFont (Writablefont.createfont ("Arial"), One, Writablefont.bold,false, Underlinestyle.no_underline, Jxl.format.Colour.BLACK); Jxl.write.WritableCellFormat WCFFC=NewJxl.write.WritableCellFormat (WFC);//Table StyleWcffc.setalignment (Jxl.format.Alignment.CENTRE);//Center HorizontallyWcffc.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);//Center VerticallyWcffc.setborder (Border.all, Borderlinestyle.thin);//LinesWritableworkbook WB = Workbook.createworkbook (out);//in the Write streamWritablesheet ws = Wb.createsheet ("Sheet1", 1); int[] GeSHi = {30,20,20};//Get column width             for(inti = 0; i < geshi.length; i++) {Ws.setcolumnview (I, geshi[i]); }                  //int ionor = 0;//Order column record value identification//Total DataJxl.write.Label name =NewJxl.write.Label (0, 0, "Interface name"), WCFFC);            Ws.addcell (name); Jxl.write.Label Count=NewJxl.write.Label (1, 0, "usage count", WCFFC);            Ws.addcell (count); Jxl.write.Label Amount=NewJxl.write.Label (2, 0, "consumption points", WCFFC);                            Ws.addcell (amount); //Show data after the second row//format data after the second rowWFC =NewJxl.write.WritableFont (Writablefont.arial, ten, Writablefont.no_bold,false, Underlinestyle.no_underline, Colour.black); WCFFC=NewWritablecellformat (WFC); Wcffc.setalignment (Jxl.format.Alignment.CENTRE);//Center HorizontallyWcffc.setverticalalignment (Jxl.format.VerticalAlignment.CENTRE);//Center VerticallyWcffc.setborder (Border.all, Borderlinestyle.thin);//Lines                        intIonor = 0;//Order column record value identificationInteger addupto = 0;  for(Interfacecost interfacecost:interfacecostlistnew) {Jxl.write.Label Opttype=NewJxl.write.Label (0, Ionor + 1, Interfacecost.getinterfacename (), WCFFC);                                Ws.addcell (Opttype); Jxl.write.Number Totlecount=NewJxl.write.Number (1, Ionor + 1, Interfacecost.gettotlecount (), WCFFC);                                Ws.addcell (Totlecount); Jxl.write.Number Totle=NewJxl.write.Number (2, Ionor + 1, Interfacecost.gettotlecost (), WCFFC);                                Ws.addcell (Totle); Addupto+=Interfacecost.gettotlecost (); Ionor++; //ws.mergecells (0, Ionor, 1, ionor);//Merge Cells} Ionor=ionor+1; Jxl.write.Label Numbername=NewJxl.write.Label (0, Ionor, "Total consumption" +addupto.tostring () + "points", WCFFC);                        Ws.addcell (Numbername); Ws.mergecells (0, Ionor, 2, Ionor);//Merge CellsWb.write (); Wb.close ();//out.close ();                    } Catch(Exception ex) {ex.printstacktrace (); }finally{            if(out!=NULL) {out.close (); }        }            //read from diskInputStream instream =NULL; OutputStream Outs=NULL; Try{Response.setcontenttype ("Application/vnd.ms-excel"); Response.AddHeader ("Content-disposition", "attachment; Filename= "+ file_name +" "); Instream=NewFileInputStream (uploaddir+file_name);//String linetxt = null;Outs =Response.getoutputstream (); byte[] buf =New byte[4096]; intreadlength;  while(((Readlength = Instream.read (BUF))! =-1) {outs.write (buf,0, readlength); }//instream.close ();Outs.flush ();//outs.close ();}Catch(Exception e) {log.error ("Error reading file contents");         E.printstacktrace (); }finally{             if(out!=NULL) {out.close (); }             if(instream!=NULL) {instream.close (); }         }                return NULL; }

Java Report download code excel

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.