js 导出csv格式文件,并且处理逗号

这篇博客介绍了如何在JavaScript中导出CSV格式的文件,特别是当字段内容包含逗号时,如何通过在字段周围添加引号来正确处理这种情况。文中展示了通过遍历数据并构建CSV字符串的方法,确保每个含有逗号的字段都被引号包围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js 导出csv格式文件,如果字段中存在逗号,将整个字段拼接上引号:如下
if(!Curent_noflyZoneList||Curent_noflyZoneList.length==0){
layerOpen(“没有可导出的数据!”);
return;
}
var obj={};
obj.title=[“序号”,“禁飞区名称”,“类型”,“半径”,“起始方位”,“截止方位”,“长度”,“宽度”,“离地高度”,“海拔高度”,“位置点”,“备注”];
obj.titleForKey=[“number”,“name”,“type”,“round_radius”,“sector_startLocation”,“sector_endLocation”,“ellipse_length”,“ellipse_width”,“height”,“altitude”,“locationList”,“remarks”];
var dataTmp=[];
for(let k in Curent_noflyZoneList){
let data={};
data.number=k+1;
data.name=Curent_noflyZoneList[k].name;
data.type=“多边形”;
data.round_radius="";
data.sector_startLocation="";
data.sector_endLocation="";
data.ellipse_length="";
data.ellipse_width="";
data.height=Curent_noflyZoneList[k].height;
data.altitude=Curent_noflyZoneList[k].altitude;
data.remarks=“多边形至少3个点”;
let locationListTmp=Curent_noflyZoneList[k].locationList;
let list="";
if(locationListTmp&&locationListTmp.length>0){
list=locationListTmp[0].longitude+","+locationListTmp[0].latitude;
if(locationListTmp.length>2){
for(let j=1;j< locationListTmp.length;j++){
let lngLat =locationListTmp[j].longitude+","+locationListTmp[j].latitude;
list=list+";"+lngLat;
}
}

    }
    //这么处理,前面加引号的转义,末尾也加上,就行了
    data.locationList="\""+list+"\"";
    dataTmp.push(data);
}
obj.data=dataTmp;
/*
* csv导入格式
* {
              title:["第一列","第二列"],
              titleForKey:["num1","num2"],
              data:[
                 {
                  num1:"123",
                  num2:"fff"
                 },{
                  num1:"123",
                  num2:"fff"
                 },{
                  num1:"123",
                  num2:"fff"
                 }]
          }

* */
//title ["","",""]
var title = obj.title;
//titleForKey ["","",""]
var titleForKey = obj.titleForKey;
var data = obj.data;
var str = [];
str.push(obj.title.join(",")+"\n");
for(var i=0;i<data.length;i++){
    var temp = [];
    for(var j=0;j<titleForKey.length;j++){
        temp.push(data[i][titleForKey[j]]);
    }
    str.push(temp.join(",")+"\n");
}
var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str.join(""));
var downloadLink = document.createElement("a");
downloadLink.href = uri;
//以日期为文件名
//以日期为文件名
var currDate = new Date(); //2018
var year =(currDate.getFullYear()<10?("0"+currDate.getFullYear()):currDate.getFullYear()).toString();
var month=((currDate.getMonth()+1)<10?("0"+currDate.getMonth()+1):currDate.getMonth()+1).toString();
var Date1=(currDate.getDate()<10?("0"+currDate.getDate()):currDate.getDate()).toString();
var Hours=(currDate.getHours()<10?("0"+currDate.getHours()):currDate.getHours()).toString();
var Minutes=(currDate.getMinutes()<10?("0"+currDate.getMinutes()):currDate.getMinutes()).toString();
var Seconds=(currDate.getSeconds()<10?("0"+currDate.getSeconds()):currDate.getSeconds()).toString();
var excelName = year+month+Date1+Hours+Minutes+Seconds;
downloadLink.download = excelName+".csv";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值