jquery.fileDownload.js插件导出excel 推荐
2017-06-13 11:50
615 查看
因为使用ajax导出excel会出现问题,所以现在使用jQuery.fileDownload.js插件来解决导出excel的问题 http://johnculviner.com/ href="http://lib.csdn.net/base/jquery" target=_blank>jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/
在页面引入jquery.fileDownload.js插件
1、如下所示
public
void export(final HttpServletRequest request, HttpServletResponse
response,final String semesterId) throws IOException,
IllegalArgumentException, IllegalAccessException {
String fileName = "excel文件";
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"));
//在这里加入设置Cookie -------------
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、如果要使回调函数successCallback和failCallback起作用,还得在后台代码中返回Cookie
jquery-file-Download.js源码解析:
后台设置与特定的cookie值
前台js定时去调用checkFileDownloadComplete方法,检查前台与后台返回的cookie值是否匹配
在页面引入jquery.fileDownload.js插件
1、如下所示
<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("正在导出,请稍后..."); }, successCallback: function(url){ alert("导出完成!"); }, failCallback: function (html, url) { alert("导出失败,未知的异常。"); } }); });jquery-file-Download.js源码解析:
onPrepare: function (url) { //preparingMessageHtml属性存在,弹出导出准备提示框 if (settings.preparingMessageHtml) { //jueryUi dialog 可自己修改成其它的。 $preparingDialog = $("<div>").html(settings.preparingMessageHtml).dialog(settings.dialogOptions); } else if (settings.prepareCallback) { //调用回调函数 settings.prepareCallback(url); } }, //导出失败调用的函数 onFail: function (responseHtml, url, error) { //准备提示对话框存在则关闭 if ($preparingDialog) { $preparingDialog.dialog('close'); } //failMessageHtml存在弹出对话框提示 if (settings.failMessageHtml) { $("<div>").html(settings.failMessageHtml).dialog(settings.dialogOptions); } //调用回调函数 settings.failCallback(responseHtml, url, error); deferred.reject(responseHtml, url); }2、在后台代码中设置Cookie,并返回Cookie的值
public
void export(final HttpServletRequest request, HttpServletResponse
response,final String semesterId) throws IOException,
IllegalArgumentException, IllegalAccessException {
String fileName = "excel文件";
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"));
//在这里加入设置Cookie -------------
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、如果要使回调函数successCallback和failCallback起作用,还得在后台代码中返回Cookie
jquery-file-Download.js源码解析:
后台设置与特定的cookie值
前台js定时去调用checkFileDownloadComplete方法,检查前台与后台返回的cookie值是否匹配
//check if the file download has completed every checkInterval ms setTimeout(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) > -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; }
相关文章推荐
- 使用jquery.fileDownload.js插件导出excel
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹) 推荐
- jQuery ajaxFileUpload.js 插件 ie9 下bug 修复
- 一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子
- jquery.fileupload.js插件使用--多视频或音频上传预览
- jQuery ajax + ajaxfileupload.js插件 实现无刷新文件上传
- 纯js的excel导出插件
- jquery使用jxl插件导出excel示例
- 一个不错的能将HTML表格导出为excel,pdf等的jquery插件
- jquery.fileupload.js插件使用--前台上传预览辅助插件类
- jquery.fileupload.js插件使用初探--单图片上传预览
- jquery.nicescroll.js可全屏可改滚动条颜色的滚动条插件-推荐
- 推荐一个挺好用的jquery插件,jquery.jqplot.js(画图),此处简略得说一下折线图的画法
- jQuery.fileDownload.js successCallback方法失效的问题
- jquery.fileupload.js插件使用--后台处理单个上传文件类
- jquery.fileEveryWhere.js 一个跨浏览器的file显示插件
- jQuery 关于ajaxfileupload.js插件的逐步解析(ajaxfileupload.js第二弹)
- jquery.fileupload.js插件使用初探--多图片上传预览
- excel导出插件失败解决(原标题JS new ActiveXObject("Excel.Application")产生的问题)
- jQuery插件ajaxfileupload.js实现上传文件