我已将excel文件保存为.csv知道我想将该数据导入HTML表格
e.g。 .csv文件中的数据为
Name, Father's Name, Roll No., Attendance
abc,xyz,11011519-059,P
abs,ijk,11011519-007,A
此代码工作正常。它从.csv或.txt文件成功读取数据但我的问题是我想要HTML表中的数据。您能否提供一个我可以在此代码上实现的简单解决方案。
<!DOCTYPE html>
<html>
<head>
<title>Read File (via User Input selection)</title>
<script type="text/javascript">
var reader; //GLOBAL File Reader object for demo purpose only
/**
* Check for the various File API support.
*/
function checkFileAPI() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
reader = new FileReader();
return true;
} else {
alert('The File APIs are not fully supported by your browser. Fallback required.');
return false;
}
}
/**
* read text input
*/
function readText(filePath) {
var output = ""; //placeholder for text output
if (filePath.files && filePath.files[0]) {
reader.onload = function (e) {
output = e.target.result;
displayContents(output);
};//end onload()
reader.readAsText(filePath.files[0]);
}//end if html5 filelist support
else { //this is where you could fallback to Java Applet, Flash or similar
return false;
}
return true;
}
/**
* display content using a basic HTML replacement
*/
function displayContents(txt) {
var el = document.getElementById('main');
el.innerHTML = txt; //display output in DOM
}
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:2)
function makeTable ( csv ) {
var rows = csv.split('\n'),
table = document.createElement('table'),
tr = null, td = null,
tds = null;
for ( var i = 0; i < rows.length; i++ ) {
tr = document.createElement('tr');
tds = rows[i].split(',');
for ( var j = 0; j < tds.length; j++ ) {
td = document.createElement('td');
td.innerHTML = tds[j];
tr.appendChild(td);
}
table.appendChild(tr);
}
document.appendChild(table);
}