纯前端JS导出数据为excel----2
来源:互联网 发布:我们如何看待网络用语 编辑:程序博客网 时间:2024/05/21 22:33
今天又整了一个导出文件的js,废话不说,大家看代码吧,
觉得不好用的可以参考我上一篇文章 http://blog.csdn.net/ymm_0008/article/details/72723476
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="../jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#Export').click(function(){
var data = {
"title":[
{"value":"集团", "type":"ROW_HEADER_HEADER", "datatype":"string"},
{"value":"创始人", "type":"ROW_HEADER_HEADER", "datatype":"string"}
],
"data":[
[
{"value":"阿里巴巴", "type":"ROW_HEADER"},
{"value":"马云", "type":"ROW_HEADER"}
]
]
};
if(data == '')
return;
JSONToExcelConvertor(data.data, "Report", data.title);
});
});
function JSONToExcelConvertor(JSONData, FileName, ShowLabel) {
//先转化json
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
var excel = '<table>';
//设置表头
var row = "<tr>";
for (var i = 0, l = ShowLabel.length; i < l; i++) {
row += "<td>" + ShowLabel[i].value + '</td>';
}
//换行
excel += row + "</tr>";
//设置数据
for (var i = 0; i < arrData.length; i++) {
var row = "<tr>";
for (var index in arrData[i]) {
var value = arrData[i][index].value === "." ? "" : arrData[i][index].value;
row += '<td>' + value + '</td>';
}
excel += row + "</tr>";
}
excel += "</table>";
var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = FileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</head>
<body>
<input type="button" id="Export" value="导出" />
</body>
</html>
阅读全文
1 0
- 纯前端JS导出数据为excel----2
- 纯JS导出文件为excel格式-----1
- js前端导出Excel
- js前端导出excel表格
- 纯前端利用 js-xlsx 实现 Excel 文件导入导出功能示例
- 纯前端利用 js-xlsx 实现 Excel 文件导入导出功能示例
- 纯前端利用 js-xlsx 实现 Excel 文件导入导出功能示例
- 纯js的excel导出插件
- 纯JS导出excel(支持中文)
- 纯js 解决页面table导出excel
- Js导出Table为Excel
- JS 导出Table为excel
- js导出tables为excel
- 无服务端纯前台导出数据到Excel-JSExcelXML.js 使用指南
- DataGridView数据导出为Excel
- DataGridView 数据导出为EXCEL
- plsql 导出数据为excel
- 导出数据为excel表格
- 简单实用的双向电平转换电路(非常实用!)
- ubuntu没网环境安装libreoffice和unoconv,用以word转pdf用
- 【Linux\云服务器】CentOS 7.2 64位 可视化桌面的安装与卸载
- Kotlin入门教程系列(一)Kotlin HelloWorld
- 8、js插件之MagicZoom.js放大镜插件
- 纯前端JS导出数据为excel----2
- 【LeetCode】partition-list
- HTML的footer置于页面最底部的方法
- matlab 车牌识别 程序
- TEE: OP-TEE
- IT人员健康信号之大脑保养
- sqoop1.99.7create job的注意点
- spring EhCache缓存之annotation注解
- 自定义的巡航栏