Javascript 将json对象转换为csv文件,并使用JSZip将其合并
我正在使用下面的函数将JSON对象转换为CSV文件并下载它。这部分做得很好Javascript 将json对象转换为csv文件,并使用JSZip将其合并,javascript,jszip,Javascript,Jszip,我正在使用下面的函数将JSON对象转换为CSV文件并下载它。这部分做得很好 const JSONToCSVConvertor = (JSONData, ReportTitle, ShowLabel) => { //If JSONData is not an object then JSON.parse will parse the JSON string in an Object var arrData = typeof JSONData !== 'obje
const JSONToCSVConvertor = (JSONData, ReportTitle, ShowLabel) => {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData =
typeof JSONData !== 'object' ? JSON.parse(JSONData) : JSONData;
var CSV = '';
//This condition will generate the Label/Header
if (ShowLabel) {
var row = '';
//This loop will extract the label from 1st index of on array
for (var index in arrData[0]) {
//Now convert each value to string and comma-seprated
row += index + ',';
}
row = row.slice(0, -1);
//append Label row with line break
CSV += row + '\r\n';
}
//1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = '';
//2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
}
row.slice(0, row.length - 1);
//add a line break after each row
CSV += row + '\r\n';
}
if (CSV === '') {
alert('Invalid data');
return;
}
//Generate a file name
var fileName = '';
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g, '_');
//Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);
// generate a temp <a /> tag
var link = document.createElement('a');
link.href = uri;
//set the visibility hidden so it will not effect on your web-layout
link.style = 'visibility:hidden';
link.download = fileName + '.csv';
//this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// console.log('finish download one file');
};
const JSONToCSVConvertor=(JSONData、ReportTitle、ShowLabel)=>{
//如果JSONData不是对象,那么JSON.parse将解析对象中的JSON字符串
var arrData=
typeof JSONData!=“object”?JSON.parse(JSONData):JSONData;
var CSV=“”;
//此条件将生成标签/标题
如果(显示标签){
var行=“”;
//此循环将从数组的第一个索引中提取标签
对于(arrData[0]中的var索引){
//现在将每个值转换为字符串和逗号分隔符
行+=索引+',';
}
row=row.slice(0,-1);
//用换行符追加标签行
CSV+=行+'\r\n';
}
//第一个循环是提取每一行
对于(变量i=0;i
现在我正在尝试基于JSON对象创建一个zip文件。我计划使用JSZip库
for (var i = 0; i < 5; i++) {
zip.file('file' + i + '.csv', escape(CSV));
}
zip.generateAsync({ type: 'base64' }).then((base64) => {
window.location = 'data:application/zip;base64,' + base64;
});
for(变量i=0;i<5;i++){
zip.file('file'+i+'.csv',escape(csv));
}
generateAsync({type:'base64'})。然后((base64)=>{
window.location='data:application/zip;base64'+base64;
});
escape(CSV)
在上面的代码片段中是正确的CSV文件格式。此代码段不会创建zip文件,也不会抛出任何错误代码。它只会使我的当前页面变成:空白#阻塞
但是,如果我将zip.file()的第二个参数更改为“csv数据”,它会工作并提供一个zip文件
我的解决办法是
JSONToCSVConvertor
功能下载CSV文件我认为这种方式不是最好的方式,因为我必须在压缩多个CSV文件之前下载它们。为了回答我的问题,我跟随本文来解决我的问题