简体   繁体   English

如何使用JavaScript生成正确的CSV文件格式

[英]How to generate proper csv file format using javascript

I have written a JavaScript code which should parse a table and prints values in a div in csv format. 我写了一个JavaScript代码,该代码应该解析一个表并以CSV格式打印div中的值。

My code : 我的代码:

 for (i = 1; i < table.rows.length; i += 1)
    { 
           row = table.rows[i];
           for (j = 1; j < row.cells.length; j += 1) 
           {
               cell = row.cells[j];
               var heatmapval=(cell.innerHTML).trim();     
               if(heatmapval>25)
               {
                   heatmapval=25;
               }       
               var arr = new Array(3);
               arr[0] = i-1;
               arr[1] = j-1;
               arr[2] = heatmapval;
               $("#csv").append(arr.join(",")+"<br>");

           }
        }

Which results perfect as it prints in the same form in div. 结果完美,因为它以div的相同形式打印。 I am using highcharts for heatmaps which reads csv values and should create heatmaps for the values.(Note: I have added the link for fiddle in comments).But it can't read my code generated csv form values in div. 我正在使用用于读取csv值的热图的highcharts,并应该为这些值创建热图。(注意:我已经在注释中添加了小提琴的链接),但是它无法读取我在div中生成的csv表单值的代码。

As I have added a break tag to go to next line,probably because of that heatmaps can't read those values .I have tried it with a space instead of break but even that does't work. 当我添加了一个break标签以转到下一行时,可能是因为热图无法读取这些值。我尝试使用空格而不是break来尝试它,但即使这样也无法正常工作。

Change 更改

 $("#csv").append(arr.join(",")+"<br>");

TO

 $("#csv").append(arr.join(",")+"\r\n");

<br> is a html tag where as \\r\\n is end of line and next line characters. <br>是html标记,其中\\r\\n是行尾和下一行字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM