我知道有很多这种性质的问题,但是我需要使用JavaScript来完成。我正在使用Dojo 1.8并将所有属性信息都放在数组中,如下所示:
Dojo 1.8
[["name1", "city_name1", ...]["name2", "city_name2", ...]]
知道如何将其导出到CSV客户端吗?
CSV
您可以使用本机JavaScript执行此操作。您必须这样将数据解析为正确的CSV格式(假设您正在使用问题中所描述的数组数组):
const rows = [ ["name1", "city1", "some other info"], ["name2", "city2", "more info"] ]; let csvContent = "data:text/csv;charset=utf-8,"; rows.forEach(function(rowArray) { let row = rowArray.join(","); csvContent += row + "\r\n"; });
或更短的方法(using arrow functions):
const rows = [ ["name1", "city1", "some other info"], ["name2", "city2", "more info"] ]; let csvContent = "data:text/csv;charset=utf-8," + rows.map(e => e.join(",")).join("\n");
然后,您可以使用JavaScript window.open和encodeURI函数下载CSV文件,如下所示:
window.open
encodeURI
var encodedUri = encodeURI(csvContent); window.open(encodedUri);
如果要给文件指定一个特定的名称,则必须做一些不同的事情,因为不支持使用window.open方法访问数据URI 。为了实现这一点,您可以创建一个隐藏的<a>DOM节点并按download如下所示设置其属性:
<a>
download
var encodedUri = encodeURI(csvContent); var link = document.createElement("a"); link.setAttribute("href", encodedUri); link.setAttribute("download", "my_data.csv"); document.body.appendChild(link); // Required for FF link.click(); // This will download the data file named "my_data.csv".