从某个位置读取 csv 文件并显示为 html 表 [英] read csv file from a location and display as html table
问题描述
有没有办法让 HTML 页面从特定目的地读取 csv 文件并将其显示为网页中的 HTML 表格?
Is there any way to get an HTML page to read a csv file from a particular destination and display it as an HTML table in the web page?
我正在开发一个网页来显示登录用户的状态.因此 HTML 页面必须自动读取 csv 文件并相应地将其显示在网页中,因为 csv 文件将定期更新.
I am developing a web page to display the status of users that are logged in. Therefore the HTML page has to automatically read the csv file and display it in a web page accordingly, since the csv file will be updated periodically.
根据答案中的建议添加了代码,但浏览器中没有弹出任何内容
Added the code as per suggestion from the answer, but nothing pops up in the browser
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSV Downloader</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.2/papaparse.js"></script>
<script>
function arrayToTable(tableData) {
var table = $('<table></table>');
$(tableData).each(function (i, rowData) {
var row = $('<tr></tr>');
$(rowData).each(function (j, cellData) {
row.append($('<td>'+cellData+'</td>'));
});
table.append(row);
});
return table;
}
$.ajax({
type: "GET",
url: "C:Users im tomDesktopcsvTOhtmldata.csv",
success: function (data) {
$('body').append(arrayToTable(Papa.parse(data).data));
}
});
</script>
</body>
推荐答案
三步流程
您需要三个步骤:
Three step process
You need three steps :
1) 使用 Ajax 从您的服务器获取数据并将其转换为数组.你可以这样做,例如.使用以下 jQuery :
1) Use Ajax to fetch data from your server and turn it into an array. You could do this eg. with the following jQuery :
$.ajax({
type: "GET",
url: "data.csv",
success: CSVToHTMLTable
});
2) 获得 CSV 文件后,您需要对其进行解析.一个简单的 &可靠的方法是使用像 Papa Parse 之类的库:
2) Once you have get your CSV file, you need to parse it. An easy & reliable way to do it, would be to use a library like Papa Parse :
var data = Papa.parse(data).data;
3) 您需要定义一个函数来将您的数组转换为 HTML 表格.以下是使用 jQuery 执行此操作的方法:
3) You need to define a function to transform your array into an HTML table. Here's how you could do this with jQuery :
function arrayToTable(tableData) {
var table = $('<table></table>');
$(tableData).each(function (i, rowData) {
var row = $('<tr></tr>');
$(rowData).each(function (j, cellData) {
row.append($('<td>'+cellData+'</td>'));
});
table.append(row);
});
return table;
}
把所有东西放在一起
以下是将所有内容组合在一起的方法:
Putting everything together
Here's how you can put everything together :
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.2/papaparse.js"></script>
<script>
function arrayToTable(tableData) {
var table = $('<table></table>');
$(tableData).each(function (i, rowData) {
var row = $('<tr></tr>');
$(rowData).each(function (j, cellData) {
row.append($('<td>'+cellData+'</td>'));
});
table.append(row);
});
return table;
}
$.ajax({
type: "GET",
url: "http://localhost/test/data.csv",
success: function (data) {
$('body').append(arrayToTable(Papa.parse(data).data));
}
});
</script>
这篇关于从某个位置读取 csv 文件并显示为 html 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!