js基于FileSaver.js 浏览器导出Excel文件的示例
程序员文章站
2023-02-23 23:40:33
本文介绍了js基于filesaver.js 浏览器导出excel文件,分享给大家,也给自己做个笔记
限制一:不同浏览器对 blob 对象有不同的限制
具体看看下...
本文介绍了js基于filesaver.js 浏览器导出excel文件,分享给大家,也给自己做个笔记
限制一:不同浏览器对 blob 对象有不同的限制
具体看看下面这个表格(出自filesaver.js):
browser | constructs as | filenames | max blob size | dependencies |
---|---|---|---|---|
firefox 20+ | blob | yes | 800 mib | none |
firefox < 20 | data: uri | no | n/a | blob.js |
chrome | blob | yes | 500 mib | none |
chrome for android | blob | yes | 500 mib | none |
edge | blob | yes | ? | none |
ie 10+ | blob | yes | 600 mib | none |
opera 15+ | blob | yes | 500 mib | none |
opera < 15 | data: uri | no | n/a | blob.js |
safari 6.1+* | blob | no | ? | none |
safari < 6 | data: uri | no | n/a | blob.js |
限制二:构建完 blob 对象后才会转换成文件
这一点限制对小文件(几十kb)可能没什么影响,但对稍微大一点的文件影响就很大了。试想,用户要下载一个 100mb 的文件,如果他点击了下载按钮之后没看到下载提示的话,他肯定会继续按,等他按了几次之后还没看到下载提示时,他就会抱怨我们的网站,然后离开了。
然而事实上下载的的确确发生了,只是要等到下载完文件之后才能构建 blob 对象,再转化成文件。而且,用户再触发多几次下载就会造成一些资源上的浪费。
因此,如果是要下载大文件的话,还是推荐直接创建一个 <a> 标签拉~
写 html 也好,写 javascript 动态创建也好,用自己喜欢的方式去创建就好了。
filesaver.js
/* filesaver.js * a saveas() filesaver implementation. * 1.3.2 * 2016-06-16 18:25:19 * * license: mit * see https://github.com/eligrey/filesaver.js/blob/master/license.md */ /*global self */ /*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ /*! @source http://purl.eligrey.com/github/filesaver.js/blob/master/filesaver.js */ var saveas = saveas || (function(view) { "use strict"; // ie <10 is explicitly unsupported if (typeof view === "undefined" || typeof navigator !== "undefined" && /msie [1-9]\./.test(navigator.useragent)) { return; } var doc = view.document // only get url when necessary in case blob.js hasn't overridden it yet , get_url = function() { return view.url || view.webkiturl || view; } , save_link = doc.createelementns("http://www.w3.org/1999/xhtml", "a") , can_use_save_link = "download" in save_link , click = function(node) { var event = new mouseevent("click"); node.dispatchevent(event); } , is_safari = /constructor/i.test(view.htmlelement) || view.safari , is_chrome_ios =/crios\/[\d]+/.test(navigator.useragent) , throw_outside = function(ex) { (view.setimmediate || view.settimeout)(function() { throw ex; }, 0); } , force_saveable_type = "application/octet-stream" // the blob api is fundamentally broken as there is no "downloadfinished" event to subscribe to , arbitrary_revoke_timeout = 1000 * 40 // in ms , revoke = function(file) { var revoker = function() { if (typeof file === "string") { // file is an object url get_url().revokeobjecturl(file); } else { // file is a file file.remove(); } }; settimeout(revoker, arbitrary_revoke_timeout); } , dispatch = function(filesaver, event_types, event) { event_types = [].concat(event_types); var i = event_types.length; while (i--) { var listener = filesaver["on" + event_types[i]]; if (typeof listener === "function") { try { listener.call(filesaver, event || filesaver); } catch (ex) { throw_outside(ex); } } } } , auto_bom = function(blob) { // prepend bom for utf-8 xml and text/* types (including html) // note: your browser will automatically convert utf-16 u+feff to ef bb bf if (/^\s*(?:text\/\s*|application\/xml|\s*\/\s*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) { return new blob([string.fromcharcode(0xfeff), blob], {type: blob.type}); } return blob; } , filesaver = function(blob, name, no_auto_bom) { if (!no_auto_bom) { blob = auto_bom(blob); } // first try a.download, then web filesystem, then object urls var filesaver = this , type = blob.type , force = type === force_saveable_type , object_url , dispatch_all = function() { dispatch(filesaver, "writestart progress write writeend".split(" ")); } // on any filesys errors revert to saving with object urls , fs_error = function() { if ((is_chrome_ios || (force && is_safari)) && view.filereader) { // safari doesn't allow downloading of blob urls var reader = new filereader(); reader.onloadend = function() { var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;'); var popup = view.open(url, '_blank'); if(!popup) view.location.href = url; url=undefined; // release reference before dispatching filesaver.readystate = filesaver.done; dispatch_all(); }; reader.readasdataurl(blob); filesaver.readystate = filesaver.init; return; } // don't create more object urls than needed if (!object_url) { object_url = get_url().createobjecturl(blob); } if (force) { view.location.href = object_url; } else { var opened = view.open(object_url, "_blank"); if (!opened) { // apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/tools/conceptual/safariextensionguide/workingwithwindowsandtabs/workingwithwindowsandtabs.html view.location.href = object_url; } } filesaver.readystate = filesaver.done; dispatch_all(); revoke(object_url); } ; filesaver.readystate = filesaver.init; if (can_use_save_link) { object_url = get_url().createobjecturl(blob); settimeout(function() { save_link.href = object_url; save_link.download = name; click(save_link); dispatch_all(); revoke(object_url); filesaver.readystate = filesaver.done; }); return; } fs_error(); } , fs_proto = filesaver.prototype , saveas = function(blob, name, no_auto_bom) { return new filesaver(blob, name || blob.name || "download", no_auto_bom); } ; // ie 10+ (native saveas) if (typeof navigator !== "undefined" && navigator.mssaveoropenblob) { return function(blob, name, no_auto_bom) { name = name || blob.name || "download"; if (!no_auto_bom) { blob = auto_bom(blob); } return navigator.mssaveoropenblob(blob, name); }; } fs_proto.abort = function(){}; fs_proto.readystate = fs_proto.init = 0; fs_proto.writing = 1; fs_proto.done = 2; fs_proto.error = fs_proto.onwritestart = fs_proto.onprogress = fs_proto.onwrite = fs_proto.onabort = fs_proto.onerror = fs_proto.onwriteend = null; return saveas; }( typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content )); // `self` is undefined in firefox for android content script context // while `this` is nsicontentframemessagemanager // with an attribute `content` that corresponds to the window if (typeof module !== "undefined" && module.exports) { module.exports.saveas = saveas; } else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) { define("filesaver.js", function() { return saveas; }); }
调用方法
function exptoexcel(){ var content = $("#report").html(); var blob = new blob(["hello, world!"], { type: "text/plain;charset=utf-8" }); saveas(blob, "hello world.txt"); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: ES6新增的math,Number方法
推荐阅读
-
php导出csv数据在浏览器中输出提供下载或保存到文件的示例
-
javascript导出csv文件(excel)的方法示例
-
纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
-
js基于FileSaver.js 浏览器导出Excel文件的示例
-
JavaScript实现写入文件到本地的方法【基于FileSaver.js插件】
-
php导出csv数据在浏览器中输出提供下载或保存到文件的示例
-
基于Node.js的大文件分片上传示例
-
AngularJS基于http请求实现下载php生成的excel文件功能示例
-
基于流的EXCEL文件导出,SXSSFWorkbook源码解析
-
ThinkPHP框架实现导出excel数据的方法示例【基于PHPExcel】