您能否推荐一种比我下面的方法更好的使用 JavaScript 创建 .CSV 文件的方法,这是我在这里搜索以前的线程后能找到的最好的方法。一切都很好,直到我开始向数据数组添加行的负载和负载,如下所示。
var data =
[
["Thermal Store Summary:"] + "\n",
["Cylinder:", "", cylinder, "", cylinder2] + "\n",
["Heat Exchanger:", "", heatex, "", heatex2] + "\n",
["Immersion Heaters:", "", heater, "", heater2] + "\n",
**// About 30 or so entries with no problem, didn't work with any more **
];
这是存储选择菜单选项的 id 和值的变量。
var cylinder = document.getElementById("heatex").options[document.getElementById("heatex").selectedIndex].id;
var heatex = document.getElementById("heatex").options[document.getElementById("heatex").selectedIndex].id;
var heater = document.getElementById("heater").options[document.getElementById("heater").selectedIndex].id;
var cylinder2 = document.getElementById("cylinder").options[document.getElementById("cylinder").selectedIndex].value;
var heatex2 = document.getElementById("heatex").options[document.getElementById("heatex").selectedIndex].value
var heater2 = document.getElementById("heater").options[document.getElementById("heater").selectedIndex].value
这是当前创建 .CSV 文件的代码:
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach
(
function(infoArray, index)
{
dataString = infoArray;
csvContent += index < infoArray.length ? dataString+ "" : dataString;
}
);
encodedUri = encodeURI(csvContent);
至于下载本身,我曾经让它在最后自动下载,但在我尝试添加超过 30 个条目后它停止了。我将代码移动到一个单独的函数(由一个按钮调用),该函数强制下载失败,因为“没有文件”。
function download_csv()
{
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "quote.csv");
link.click();
}
任何和所有帮助表示赞赏,理想情况下是对我当前解决方案的修复,但也愿意一起尝试一种新方法。