admin管理员组文章数量:1406177
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
2 Answers
Reset to default 1You 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
本文标签: javascriptHow to read and display data from a csv file to html webpageStack Overflow
版权声明:本文标题:javascript - How to read and display data from a csv file to html webpage? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744968389a2635089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论