Export excel--pop-up box

Source: Internet
Author: User

The form submitted by the General form submission (form submission is divided into 3 kinds, see below 1, 2, 3), and set the form label enctype= "Multipart/form-data" property, then this time will open the popup box.
1. form submission 2.js form submission 3.jquery, ExtJS and so on other form submission

code example
JSP code <s:form id= "Myform1" method= "post" enctype= "Multipart/form-data" ><table width= "><tr>" <td> Export </TD><TD align= "center" ><input type= "button" value= "Export Excel Table" onclick= "Excelexpert ()"/ ></td></tr></table></s:form>

JS code function Excelexpert () {var form = document.getElementById ("Myform1"); form.action = "/projectmanagerweb/ Shangbaoaction!exportexcelofsta.action "; Form.submit ();}

Java code: Business Controller/** * Project data query exported to Excel */public void Exportexcelofsta () {try{this.setsuccess (true); System.out.println ("--Start exporting Project data query--"); list<object[]> list = Tianbaoservice.queryprojectdata (ProjectName, StartTime, EndTime, Inputuser, Organid);//t. Id,t.type,t.worktype,t.workload,t.documentpage,t.code,//t.problem,t.starttime,t.endtime,,t.intputtime, Pin.projectname, U.username "//Get Tianbaos Data Entity list<tianbaoentity> and username list<string>list< tianbaoentity> tianbaolist = new arraylist<tianbaoentity> (); list<string> usernamelist = new arraylist<string> (); list<string> projectnamelist = new arraylist<string> (); for (object[] obj:list) {tianbaoentity tianBao=new Tianbaoentity (); Tianbao.setid (String.valueof (obj[0)); Tianbao.settype (String.valueof (obj[1])); Tianbao.setworktype (string.valueof (obj[2)); Tianbao.setworkload (String.valueof (obj[3])); Tianbao.setdocumentpage (string.valueof (obj[4)); Tianbao.setcode (String.valueof (obj[5)); TianBao.setProblem ( String. ValueOf (Obj[6])); Tianbao.setstarttime (Dateutil.format (string.valueof (obj[7))); Tianbao.setendtime ( Dateutil.format (string.valueof (obj[8))); Tianbao.setintputtime (Dateutil.format (string.valueof))); Tianbaolist.add (Tianbao); String ProjectName = string.valueof (obj[10]);p rojectnamelist.add (projectname); String username = string.valueof (obj[11]); Usernamelist.add (username);} SYSTEM.OUT.PRINTLN ("Total Data:" +tianbaolist.size ()),//if (!isempty (notaskstr)) {//if (Notaskstr.equals ("Excelexpert")) {////}//}system.out.println ("Start creating table"); HttpServletResponse response = This.getresponse (); OutputStream outdata = Response.getoutputstream (); Response.reset () ; Response.setcontenttype ("APPLICATION/VND.MS-EXCEL;CHARSET=GBK"); Response.AddHeader ("Content-Disposition", " Attachment;filename= "+ New String ((" Project data Query "+ DATEUTIL.GETCURRDATESTR () +". xls "). GetBytes (" GBK ")," iso-8859-1 "); Hssfworkbook notaskexcel = new Hssfworkbook ();//define Excel cell style Hssfcellstyle cs = Notaskexcel.createcellstyle (); Cs.setalignment (hssfcellstyLe. Align_center); Cs.setborderleft (Hssfcellstyle.border_thin); Cs.setbordertop (Hssfcellstyle.border_thin); Cs.setborderright (Hssfcellstyle.border_thin); Cs.setborderbottom (Hssfcellstyle.border_thin); Hssffont f = Notaskexcel.createfont (); F.setboldweight (hssffont.boldweight_bold); F.setcharset (HSSFFont.ANSI_ CHARSET); Cs.setfont (f);//Style definition end Hssfsheet sheet = notaskexcel.createsheet ("Project data Query"); Sheet.addmergedregion (new Cellrangeaddress (0, 0, 0, 1));//Set title Hssfrow row = Sheet.createrow (0); Hssfcell cell = Row.createcell (0); if (cs! = null) {Cell.setcellstyle (CS);} Cell.setcellvalue ("Project data Query");//Set the title of the column Hssfrow Rowfirst = sheet.createrow (1); Hssfcell cell0 = Rowfirst.createcell (0); if (cs = null) {Cell0.setcellstyle (CS);} Cell0.setcellvalue ("serial number"); Hssfcell cell1 = Rowfirst.createcell (1); if (cs = null) {Cell1.setcellstyle (CS);} Cell1.setcellvalue ("project name");//third fourth fifth sixth seventh eighth ninth tenth Eleventhhssfcell CELL2 = RowFirst.create Cell (2); if (cs! = null) {Cell2.setcellstyle (CS);} Cell2.setcelLValue ("Classification"); Hssfcell Cell3 = Rowfirst.createcell (3); if (cs = null) {Cell3.setcellstyle (CS);} Cell3.setcellvalue ("type"); Hssfcell CELL4 = Rowfirst.createcell (4); if (cs = null) {Cell4.setcellstyle (CS);} Cell4.setcellvalue ("workload"); Hssfcell cell5 = Rowfirst.createcell (5); if (cs = null) {Cell5.setcellstyle (CS);} Cell5.setcellvalue ("document Pages"); Hssfcell cell6 = Rowfirst.createcell (6); if (cs = null) {Cell6.setcellstyle (CS);} Cell6.setcellvalue ("Lines of Code"); Hssfcell CELL7 = Rowfirst.createcell (7); if (cs = null) {Cell7.setcellstyle (CS);} Cell7.setcellvalue ("Number of questions"); Hssfcell Cell8 = Rowfirst.createcell (8); if (cs = null) {Cell8.setcellstyle (CS);} Cell8.setcellvalue ("Start date"); Hssfcell cell9 = Rowfirst.createcell (9); if (cs = null) {Cell9.setcellstyle (CS);} Cell9.setcellvalue ("End Date"); Hssfcell Cell10 = Rowfirst.createcell (), if (cs! = null) {Cell10.setcellstyle (CS);} Cell10.setcellvalue ("entry person"); Hssfcell cell11 = Rowfirst.createcell (one), if (cs! = null) {Cell11.setcellstyle (CS);} Cell11.setcellvalue ("date entered"); for (int i = 0; I < tianbaolist.size (); i++) {tianbaoentity Tianbao = (tianbaoentity) tianbaolist.get (i); Hssfrow row1 = Sheet.createrow (i + 2); Hssfcell cl0 = Row1.createcell (0); if (cs = null) {Cl0.setcellstyle (CS);} Cl0.setcellvalue (i + 1); Hssfcell CL1 = Row1.createcell (1); if (cs = null) {Cl1.setcellstyle (CS);} Cl1.setcellvalue (Projectnamelist.get (i)); Hssfcell Cl2 = Row1.createcell (2); if (cs = null) {Cl2.setcellstyle (CS);} Classification renderstring type = Tianbao.gettype (); String typetemp = null;if (Type.equals ("Promanager")) {typetemp= "project management";} else if (type.equals ("development")) {typetemp= "Product development";} else if (type.equals ("Implementation")) {typetemp= "Product implementation";} else if (type.equals ("Supportmanager")) {typetemp= "Support management";} Cl2.setcellvalue (typetemp); Hssfcell Cl3 = Row1.createcell (3); if (cs = null) {Cl3.setcellstyle (CS);} Type renderstring Worktype = Tianbao.getworktype (); String worttypetemp = null;if (worktype.equals ("plan")) {worttypetemp= "schedule";} else if (worktype.equals ("Planaduit")) {worttypetemp= "plan review";} else if (worktype.equalS ("Requiretment")) {worttypetemp= "Requirements";} else if (worktype.equals ("Requiretmentaduit")) {worttypetemp= "requirement review";} else if (worktype.equals ("Planaduit")) {worttypetemp= "plan review";} else if (worktype.equals ("Design")) {worttypetemp= ";} else if (worktype.equals ("Designaduit")) {worttypetemp= "Design Review";} else if (worktype.equals ("code")) {worttypetemp= "encoded";} else if (worktype.equals ("Codeaduit")) {worttypetemp= "code walk-through";} else if (worktype.equals ("test")) {worttypetemp= "Tests";} else if (worktype.equals ("UnitTest")) {worttypetemp= "use case review";} else if (worktype.equals ("execution")) {worttypetemp= "Implementation";} else if (worktype.equals ("cm")) {worttypetemp= "cm";} else if (worktype.equals ("QA")) {worttypetemp= "QA";} else if (worktype.equals ("other")) {worttypetemp= "others";} Cl3.setcellvalue (worttypetemp); Hssfcell Cl4 = Row1.createcell (4); if (cs = null) {Cl4.setcellstyle (CS);} String workload = Tianbao.getworkload (); Cl4.setcellvalue (Workload.equals ("null")? "": workload); Hssfcell Cl5 = Row1.createcell (5); if (cs = null) {Cl5.setcellstyle (CS);} String Documentpage = Tianbao.getdocumentpage (); String d = (documentpage.equals ("null")? "":d ocumentpage); Cl5.setcellvalue (d); Hssfcell Cl6 = Row1.createcell (6); if (cs = null) {Cl6.setcellstyle (CS);} String code = Tianbao.getcode (); Cl6.setcellvalue (Code.equals ("null")? "": Code); Hssfcell CL7 = Row1.createcell (7); if (cs = null) {Cl7.setcellstyle (CS);} String problem = Tianbao.getproblem (); Cl7.setcellvalue (Problem.equals ("null")? "":p Roblem); Hssfcell Cl8 = Row1.createcell (8); if (cs = null) {Cl8.setcellstyle (CS);} Cl8.setcellvalue (Dateutil.format (Tianbao.getstarttime ())); Hssfcell CL9 = Row1.createcell (9); if (cs = null) {Cl9.setcellstyle (CS);} Cl9.setcellvalue (Dateutil.format (Tianbao.getendtime ())); Hssfcell Cl10 = Row1.createcell (), if (cs! = null) {Cl10.setcellstyle (CS);} Cl10.setcellvalue (Usernamelist.get (i)); Hssfcell cl11 = Row1.createcell (one), if (cs! = null) {Cl11.setcellstyle (CS);} Cl11.setcellvalue (Dateutil.format (Tianbao.getintputtime ()));} Notaskexcel.write (Outdata); Outdata.flush (); Outdata.close ();} catch (EXception e) {log.error (E.getmessage (), E);}} 


Non-form submission if it is not a form submission, it must be a request. As long as it is requested, whichever of the following 1, 2, does not open the popup box--unless the request is made with window.open (URL), or in a callback function with window.open (URL) Open a window (provided that the Excel file has been generated under this path on the server's disk URL).
1.jquery Request 2.extjs Request

code Example 1
JS Code (Note: ExtJS) {columnWidth:. 1, layout: ' Form ', Border:false, items:[    {xtype: ' button ', Text: ' Export Excel table ', handler : function () {///    Ext.Ajax.request ({//    URL: '/projectmanagerweb/shangbaoaction!exportexcelofsta.action ',//          method: ' Post ',//          success:function (form,action) {//          Ext.Msg.alert ("Hint", "success");          //},// Failure:function (form,action) {//          Ext.Msg.alert ("Hint", "success");//          }//});    window.open ("/projectmanagerweb/shangbaoaction!exportexcelofsta.action");}}    ]}


code Example 2
JS Code (NOTE: jquery request) var btnexporthtml;function doexport (id) {var obj=$ ("#div_btnExport"); btnexporthtml=obj.html (); O Bj.html ("exporting files, please wait ..."); $.post ("export.jsp ", {ordid:id},function (JSON) {obj.html (btnexporthtml), if (json.success) {window.open (Json.uri," "," ");}," JSON "); return false;}

Java code: Business Controller Public String Export () throws Exception{salesorder Var=handler.retrieve (Form.getordid ()); String uri= "/exports/order/" +var.getorderno () + ". xls"; String Fn=application.getrealpath (URI); Salesorderexporter exp=new Salesorderexporter (fn,var,application); Exp.export (); json= "{\" success\ ": True,\" uri\ ": \ "+uri+" \ "}"; return SUCCESS;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Export excel--pop-up box

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.