json转excel js
来源:互联网 发布:网络生活服务类平台 编辑:程序博客网 时间:2024/06/05 03:40
function exportExcel(FileName = '数据统计', JSONData = [], columns = []) {
let excel = '<table>';
//设置表头
let row = '<tr>';
for (let i = 0, l = columns.length; i < l; i+=1) {
row += `<td>${columns[i].title}</td>`;
}
//换行
excel += `${row}</tr>`;
//设置数据
for (let i = 0; i < JSONData.length; i+=1) {
let row = '<tr>';
for (let j = 0; j < columns.length; j+=1) {
let value = JSONData[i][columns[j].key];
row += `<td>${value}</td>`;
}
excel += `${row}</tr>`;
}
excel += '</table>';
const 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'>
<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
<head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>
{worksheet}
</x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body>
${excel}
</body>
</html>
`;
const link = document.createElement('a');
link.href = `data:application/vnd.ms-excel;charset=utf-8,${encodeURIComponent(excelFile)}`;
link.style = 'visibility:hidden';
link.download = `${FileName}.xls`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
let excel = '<table>';
//设置表头
let row = '<tr>';
for (let i = 0, l = columns.length; i < l; i+=1) {
row += `<td>${columns[i].title}</td>`;
}
//换行
excel += `${row}</tr>`;
//设置数据
for (let i = 0; i < JSONData.length; i+=1) {
let row = '<tr>';
for (let j = 0; j < columns.length; j+=1) {
let value = JSONData[i][columns[j].key];
row += `<td>${value}</td>`;
}
excel += `${row}</tr>`;
}
excel += '</table>';
const 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'>
<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">
<head>
<!--[if gte mso 9]>
<xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>
{worksheet}
</x:Name>
<x:WorksheetOptions>
<x:DisplayGridlines/>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
</x:ExcelWorkbook>
</xml>
<![endif]-->
</head>
<body>
${excel}
</body>
</html>
`;
const link = document.createElement('a');
link.href = `data:application/vnd.ms-excel;charset=utf-8,${encodeURIComponent(excelFile)}`;
link.style = 'visibility:hidden';
link.download = `${FileName}.xls`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
注: const columns = [
{
key: 'title',
title: '报告名称',
}, {
key: 'type',
title: '类型',
}]
const data = [{title: 'xxxx', type='sda'}]
阅读全文
0 0
- js json转excel
- json转excel js
- Excel转JSON
- json转Excel
- Excel转Json
- Js Json字符串转json
- Excel2JSON Excel转JSON Excel另存为JSON的技巧
- java之excel 转 json
- python实现Excel转json
- c# excel表格转Json
- JSON转EXCEL代码( java)
- Excel(xls)转json工具
- phpexcel将json转excel
- js操作excel(转)
- Js字符串转json
- js数组转json
- Node.js 读取excel文件转换成json格式
- js 获取excel文件并以json形式输出
- jeesite 重置按钮
- Unity3D 性能优化
- Java基础之抽象类和继承知识点总结
- Leetcode 算法习题 第十四周
- xilinx vivado debug
- json转excel js
- libwebsockets 交叉编译
- 剑指offer---旋转数组中最小的数字(8)
- 源码网站收藏
- python reshape -1
- 使用jquery实现多张图片的上传
- 移动端 js new Date 根据日期获取日期问题 Invalid date 、undefined,
- Android写xml布局时的注意事项
- Caffe学习笔记(一):CIFRA-10在Caffe上进行训练学习