javascript - How to read and display data from a csv file to html webpage? - Stack Overflow

I am new to html coding. I want to read and display my csv file data to a webpage. Data in each row of

I am new to html coding. I want to read and display my csv file data to a webpage. Data in each row of csv file must be displayed in a separate line on webpage. My current code doesn't display anything and I don't have any clue why is it not working properly. Please help me in this regard. My code is below:

    <!DOCTYPE html>
<html>
<script type="text/javascript">
    var allText =[];
    var allTextLines = [];
    var Lines = [];

    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "D:\PycharmProjects\filename.csv", true);
    txtFile.onreadystatechange = function()
    {
        allText = txtFile.responseText;
        allTextLines = allText.split(/\r\n|\n/);
    };

    document.write(allTextLines);<br>
    document.write(allText);<br>
    document.write(txtFile);<br>
</script> 

I am new to html coding. I want to read and display my csv file data to a webpage. Data in each row of csv file must be displayed in a separate line on webpage. My current code doesn't display anything and I don't have any clue why is it not working properly. Please help me in this regard. My code is below:

    <!DOCTYPE html>
<html>
<script type="text/javascript">
    var allText =[];
    var allTextLines = [];
    var Lines = [];

    var txtFile = new XMLHttpRequest();
    txtFile.open("GET", "D:\PycharmProjects\filename.csv", true);
    txtFile.onreadystatechange = function()
    {
        allText = txtFile.responseText;
        allTextLines = allText.split(/\r\n|\n/);
    };

    document.write(allTextLines);<br>
    document.write(allText);<br>
    document.write(txtFile);<br>
</script> 
Share Improve this question edited Jan 13, 2017 at 14:51 Goombah 2,8552 gold badges14 silver badges22 bronze badges asked Jan 13, 2017 at 14:48 ddopddop 231 gold badge1 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You can use following javascript for read and display data from csv file

<script type="text/javascript">
$.get('/file-path/demo.csv', function(data) {
var build = '<table border="1" cellpadding="2" cellspacing="0" style="border-collapse: collapse" width="100%">\n';
var head = data.split("\n");
for(var i=0;i<1;i++){
build += "<tr><th>" + head[i] + "</th></tr>";
for(var i=1;i<head.length;i++){
build += "<tr><td>" + head[i].split("\n") + "</td></tr>";
}
}
build += "</table>";
$('#wrap').append(build);
});
</script>

You can iterate over the allTextLines array as soon as your document is loaded

var txtFile = new XMLHttpRequest();
txtFile.onload = function() {
    allText = txtFile.responseText;
    allTextLines = allText.split(/\r\n|\n/);

    for(var i = 0; i < allTextLines.length; i++) {
        document.body.innerHTML += allTextLines[i];
        document.body.innerHTML += '<br/>';
    }
}

txtFile.open("get", "filename.csv", true);
txtFile.send();

where filename.csv should be put in your server static resources

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744968390a4603822.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信