我无法在给定模式中将javascript数组变量值写入CSV文件

时间:2015-06-26 12:01:04

标签: javascript jquery excel jsp

我有三个名称为Area,Device和Problem的javascript变量。我想将这些变量的值逐个存储到CSV文件中,如下图所示,并单击下载按钮下载该表。在jsp和javascript中都有相同的文件。到目前为止,我已尝试过以下内容。

 Sample.jsp

<form>
      <input type="BUTTON" id="DownloadBtn" value="Download">
</form>

<script type="text/javascript">

 var Area=["Area1","Area2"];

 var Device=[["device1","device2"],["device3","device4"]];

 var Problem=[[["problem1","problem2"]],[["problem3","problem4"]],[["problem5","problem6"]],[["problem7","problem8"]]];


$("#DownloadBtn").click(function() {
        var resp=confirm("Do You Want To Download?");
            if(resp){

                 var last = '';
                  for (var i = 0; i < Area.length; i++) {
                    var first = Area[i];

                    for (var j = 0; j < first .length; j++) {
                        var second= device[j];

                        for(var k=0;k<second.length;k++){
                            var third=problem[k];

                        var value= third.replace(/"/g, '""');
                        if (value.search(/("|,|\n)/g) >= 0)
                            value= '"' + value+ '"';
                        if (k > 0)
                            last += ',';
                        last += value;
                    }
                    }
                    last += '\n';
                }

                var anchor= document.createElement('a');
                anchor.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(last ));
                anchor.setAttribute('download', 'sample.csv');
                anchor.click();
             }
});
</script>

请建议我获得所需模式所需的任何更改或修改。如何在图像中实现这一点。以下评论对此问题的任何澄清。任何建议都会受到欢迎。

enter image description here

0 个答案:

没有答案