当前位置:网站首页>js将json数组导出为excel
js将json数组导出为excel
2022-07-16 18:48:00 【spring-java】
1、定义函数
const jsonData = [
{
name:'路人甲',
phone:'123456789',
email:'[email protected]'
},
{
name:'炮灰乙',
phone:'123456789',
email:'[email protected]'
},
{
name:'土匪丙',
phone:'123456789',
email:'[email protected]'
},
{
name:'流氓丁',
phone:'123456789',
email:'[email protected]'
},
];
/**
* 导出 json 数据为 Excle 表格
* @param {json} data 要导出的 json 数据
* @param {String} head 表头, 可选 参数示例:'名字,邮箱,电话'
* @param {*} name 导出的文件名, 可选
*/
function jsonToExcel(data, head, name = '导出的文件名') {
let str = head ? head + '\n' : '';
data.forEach(item => {
// 拼接json数据, 增加 \t 为了不让表格显示科学计数法或者其他格式
for(let key in item) {
str = `${str + item[key] + '\t'},`
}
str += '\n'
});
console.log(str)
// encodeURIComponent解决中文乱码
const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
// 通过创建a标签实现
const link = document.createElement("a");
link.href = uri;
// 对下载的文件命名
link.download = `${name + '.csv'}`;
link.click();
}
2、调用
jsonToExcel(jsonData, "姓名,电话,邮箱")
边栏推荐
- MySQL - multi table query - relationship between tables
- putchar()
- Blog migration from cloudbase to virtual machine
- I2C通信协议实现在OLED显示屏数据显示
- Use of prettier code formatting tool
- MySQL --- 多表查询 - 表与表之间的关系
- R language uses pcauchy function to generate Cauchy distribution cumulative distribution function data, and uses plot function to visualize Cauchy distribution cumulative distribution function data
- Word cloud graph, word frequency graph, specially statistics the word cloud word frequency of some keywords
- Activity component export experiment
- VMware recovery snapshot failed to create an anonymous paging file of 5040 MB: insufficient system resources to complete the requested service
猜你喜欢

Initial redis (know redis and common commands)

剑指 Offer 57 - II. 和为s的连续正数序列

不同的图像patch由不同的专家模型来处理!南洋理工&Mila稀疏融合混合专家模型SF-MoE,具有超强泛化能力!代码已开源!...

Discussion on ble Bluetooth battery service

What is the event delegation in JS?

Sword finger offer 53 - I. find the number I in the sorted array

将一个文件夹中的内容复制到另一个文件夹中

Machine learning BP (back propagation) neural network

About SQL: orcale SQL, why is the foreign key inventory ID statement of the merchant table invalid

剑指 Offer 55 - I. 二叉树的深度
随机推荐
剑指 Offer 54. 二叉搜索树的第k大节点
剑指 Offer 57 - II. 和为s的连续正数序列
为什么网络安全在工厂中很重要?
关于认知分析,你需要知道的一切
居家期间撸了一款小而美的【Markdown静态博客程序】
UE4 shadow: perobjectshadow verification
VirtualBox virtual machine failed to start, e_ FAIL( 0x80004005)
[the pro test is valid]npm warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
[leetcode problem solving report] 423 Rebuild numbers from English
Use of prettier code formatting tool
[JS encapsulates a simple asynchronous API to obtain asynchronous operation results and process parsing]
R language uses LM function to build multiple regression model, writes regression equation according to model coefficient, and uses summary function to calculate the summary statistical information of
Word cloud graph, word frequency graph, specially statistics the word cloud word frequency of some keywords
[cache] introduction of a new cache caffeine cache
Unity-2d pixel lattice ablation
The concept and properties of | double integral under high numbers | high numbers | handwritten notes
Sword finger offer 53 - I. find the number I in the sorted array
R语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化直方图、使用add参数在直方图中添加均值虚线竖线、横轴添加轴须图(rug plot)
Recommend a well written article on "I2C protocol explanation"
Practical project: problems encountered in data access layer