js一键导出Excel
1 <div class="container"> 2 <table id="backViewTable" class="table table-hover table-sm table2excel"> 3 <tr> 4 <td>#</td> 5 <td>ID</td> 6 <td>姓名</td> 7 <td>座位号</td> 8 <td>操作</td> 9 </tr> 10 <tr> 11 <th scope="row">1</th> 12 <td>1234</td> 13 <td>李文斐</td> 14 <td>2楼 3排 34号 </td> 15 <td><input class="btn-primary" type="button" value="删除"/></td> 16 </tr> 17 <tr> 18 <th scope="row">2</th> 19 <td>2345</td> 20 <td>lwf</td> 21 <td>1楼 3排 34号</td> 22 <td><input class="btn-primary" type="button" value="删除"/></td> 23 </tr> 24 <tr> 25 <th scope="row">3</th> 26 <td>3456</td> 27 <td>Lee</td> 28 <td>1楼 3排 12号</td> 29 <td><input class="btn-primary" type="button" value="删除"/></td> 30 </tr> 31 </table> 32 <button class="btn btn-primary btn-sm" onclick="tablesToExcel(['backViewTable'], ['ProductDay1'], 'TestBook.xls', 'Excel')">Export to Excel</button> 33 </div>
JS:
1 <script>
2 var tablesToExcel = (function() {
3 var uri = 'data:application/vnd.ms-excel;base64,'
4 , tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
5 + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'
6 + '<Styles>'
7 + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'
8 + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'
9 + '</Styles>'
10 + '{worksheets}</Workbook>'
11 , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
12 , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'
13 , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
14 , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
15 return function(tables, wsnames, wbname, appname) {
16 var ctx = "";
17 var workbookXML = "";
18 var worksheetsXML = "";
19 var rowsXML = "";
20
21 for (var i = 0; i < tables.length; i++) {
22 if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
23
24 // 控制要导出的行数
25 for (var j = 0; j < tables[i].rows.length; j++) {
26 rowsXML += '<Row>';
27
28 // 控制导出的列数(在本例中,最后一列为button,导出的文件会出错,所以导出到倒数第二列
29 for (var k = 0; k < tables[i].rows[j].cells.length-1; k++) {
30 var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
31 var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
32 var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
33 dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
34 var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
35 dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null;
36 ctx = { attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':''
37 , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String'
38 , data: (dataFormula)?'':dataValue
39 , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':''
40 };
41 rowsXML += format(tmplCellXML, ctx);
42 }
43 rowsXML += '</Row>'
44 }
45 ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i};
46 worksheetsXML += format(tmplWorksheetXML, ctx);
47 rowsXML = "";
48 }
49
50 ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
51 workbookXML = format(tmplWorkbookXML, ctx);
52
53 // 查看后台的打印输出
54 console.log(workbookXML);
55
56 var link = document.createElement("A");
57 link.href = uri + base64(workbookXML);
58 link.download = wbname || 'Workbook.xls';
59 link.target = '_blank';
60 document.body.appendChild(link);
61 link.click();
62 document.body.removeChild(link);
63 }
64 })();
65 </script>
转载自:https://blog.csdn.net/liwenfei123/article/details/78719686
js一键导出Excel的更多相关文章
- js实现一键导出Excel
演示地址:https://xibushijie.github.io/static/ExportToExcel.html <!DOCTYPE html> <html lang=&quo ...
- 【转】js 中导出excel 较长数字串会变为科学计数法
[转]js 中导出excel 较长数字串会变成科学计数法 在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串 ...
- 多个浏览器下应用前端JS实现一键导出excel表
自己试验了几种方法,找到一种较为全面的一种方式一键输出Excel表格,代码如下 <!DOCTYPE html> <html> <head lang="en&qu ...
- js前端导出excel:json形式的导出
第一中形式的导出:主要是表头对应主体数据,json形式的导出 js库文件名称 : table2excel.js这个js库文件是网上找的,并且自己根据自己业务需求把内容改了一下复制到 table2exc ...
- js前端导出excel
此例子是利用html特性,纯前端导出excel,样式不好看,兼容主流浏览器. var tableid = jQuery(this).closest("div.tab-label-wrap&q ...
- js原生导出excel和csv
严格意义来说并不是真正的excel文件,只是可以用excel打开查看而已,实际上的格式是逗号分隔文件即csv文件. 这里有几个坑要说一下: 不加Unicode的utf8头部标识excel打开文件会 ...
- js导入导出excel
导入: <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Unti ...
- js 中导出excel 较长数字串会变成科学计数法
在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...
- js 中导出excel 较长数字串会变成科学计数法(转载)
在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...
随机推荐
- redhat 6 配置 yum 源
1.删除redhat原有的yum rpm -aq|grep yum|xargs rpm -e --nodeps 2.下载yum安装文件 注意,如果下载时找不到文件,就登录到:http://mirror ...
- commons-lang常用工具类StringEscapeUtils
原文:https://my.oschina.net/mousai/blog/88832 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止s ...
- uva 1411 Ants (权值和最小的完美匹配---KM算法)
uva 1411 Ants Description Young naturalist Bill studies ants in school. His ants feed on plant-louse ...
- owncloud
owncloud https://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ [root@n1 ~]# rpm -Uvh ius-relea ...
- VM虚拟机的网卡模式介绍
(1)Bridged方式 用这种方式,虚拟系统的IP可设置成与本机系统在同一网段,虚拟系统相当于网络内的一台.独立的机器,与本机共同插在一个Hub上,网络内其他机器可访问虚拟系统,虚拟系统也可访问网络 ...
- STM8S---外部中断应用之长按键识别
STM8经常使用中断指令 开总中断 _asm("rim"); 禁止中断 _asm("sim"); 进入停机模式 _asm("halt"); ...
- 简单区分iphone和ipad的宏定义
在公共头文件里作例如以下定义: #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 使用时: if( IS_I ...
- Android编译详解之lunch命令 【转】
本文转载自: Android编译详解之lunch命令 (2012-10-08 10:27:55) 转载▼ 标签: it 分类: android内核剖析 Android的优势就在于其开源,手机和 ...
- 美国诚实签经验——必带材料:护照,证件照,DS160确认页,面试预约确认页,+境外照片
Step3. 准备签证材料这些材料如果准备,请一定围绕着你的DS160表格,不可说谎,但可适当修饰,辅佐它,烘托它,营造出一种——你绝无可能去不复返,绝无可能制造麻烦,绝无想占人便宜的意思,并且随时可 ...
- android 使用AlarmManager定时启动service
private static AlarmManager am; private static PendingIntent pendingIntent; /** * 使用 AlarmManager 来 ...