Because there is a problem with using AJAX to export Excel, now use the JQuery.fileDownload.js plugin to solve the problem of exporting Excel
http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
Introducing the Jquery.fileDownload.js plugin on the page
1, as shown below
<script type= "Text/javascript" src= "${resource}/js/jquery.filedownload.js"/></script><script type= "Text/javascript" > $ ("#export_confirm"). On ("click", Function () {var url= "${path}/admin/information/student/ Export "; $.filedownload (url,{data:{semesterid:$ ("#misSemester"). Val ()}, Preparecallback:function (URL) {alert ("Exporting, please later ... "); }, Successcallback:function (URL) {alert ("Export complete! "); }, Failcallback:function (HTML, URL) {alert ("Export failed, unknown exception. "); } }); });
Jquery-file-download.js Source Code Analysis:
onprepare: function (URL) { //preparingmessagehtml attribute present, eject export prepare for prompt box if (settings.preparingmessagehtml) { //jueryUi dialog can modify it to other. $preparingDialog = $ ("<div>"). HTML ( settings.preparingmessagehtml). dialog (settings.dialogoptions); } else if (Settings.preparecallback) { //Call callback function settings.preparecallback (URL); }},//export failed call function Onfail : function (Responsehtml, url, error) { //Ready to prompt dialog box exists then close if ($preparingDialog) { $ Preparingdialog.dialog (' close '); } //failmessagehtml there is a popup dialog box prompt if (settings.failmessagehtml) { $ (" <div> "). HTML (settings.failmessagehtml). Dialog (Settings.dialogoptions); } //calls the callback function settings.failcallback (responsehtml, url, Error); deferred.reject (Responsehtml, url);}
2. Set the cookie in the background code and return the value of the cookie
650) this.width=650; "src=" http://blog.csdn.net/wyc111/article/details/47613465 "/>public void Export (final HttpServletRequest request, HttpServletResponse response,final String Semesterid) throws IOException, IllegalArgumentException, Illegalaccessexception {
String fileName = "Excel file";
Response.reset ();
Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8");
Response.setheader ("Content-disposition", "attachment;filename=" + New String ((filename + ". xls"). GetBytes (), " Iso-8859-1 "));
Add a setting cookie here-------------
Cookie Filedownload=new Cookie ("FileDownload", "true");
Filedownload.setpath ("/");
Response.addcookie (FileDownload);
//------------------------------------
Servletoutputstream out = Response.getoutputstream ();
Final Hssfworkbook workbook = new Hssfworkbook ();
list<future<boolean>> resultlist = new arraylist<future<boolean>> ();
3. If you want the callback function Successcallback and failcallback to work, you have to return the cookie in the background code.
Jquery-file-download.js Source Code Analysis:
Background settings with a specific cookie value
foreground JS timed to call the Checkfiledownloadcomplete method, check the foreground and background the cookie value returned is matched
check if the file download has completed every checkinterval Mssettimeout (Checkfiledownloadcomplete, settings.checkinterval); Function checkfiledownloadcomplete () { //has the cookie been written due to a File download occuring? var cookievalue = settings.cookievalue ; if (typeof cookievalue == ' string ') { cookievalue = cookievalue.tolowercase (); } var lowercasecookie = settings.cookiename.tolowercase () + "=" + cookievalue; if (Document.cookie.toLowerCase (). IndexOf (Lowercasecookie) &NBSP;>&NBSP;-1) { //execute specified callback internalcallbacks.onsuccess (FILEURL); / /remove cookie var cookiedata = settings.cookiename + "=; path=" + settings.cookiePath + " expires=" + new date (0). toUTCString () + ";"; if (settings.cookiedomain) cookieData += " domain= " + settings.cookieDomain + "; "; document.cookie = cookieData; //remove iframe cleanup ( false); return; }
This article is from the "Ciyo Technology sharing" blog, please be sure to keep this source http://ciyorecord.blog.51cto.com/6010867/1934849
Jquery.fileDownload.js Plugin Export Excel