我想浏览一个excel文件,并使用JSON格式的JavaScript读取浏览器上的文件。我想要访问工作表包含的所有元素,无论是键还是值。我不得不算。工作表的行和列。因为我必须使用密钥在我的应用程序中匹配所以我需要一个独立的访问权限。
我的代码:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.7/xlsx.core.min.js"></script>
<script>
function handleFile(e) {
//Get the files from Upload control
var files = e.target.files;
var i, f;
//Loop through files
for (i = 0, f = files[i]; i != files.length; ++i) {
var reader = new FileReader();
var name = f.name;
reader.onload = function (e) {
var data = e.target.result;
var result;
var workbook = XLSX.read(data, { type: 'binary' });
var sheet_name_list = workbook.SheetNames;
sheet_name_list.forEach(function (y) { /* iterate through sheets */
//Convert the cell value to Json
var roa = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
if (roa.length > 0) {
result = roa;
}
var roaKeys = [];
for (var i = 0; i < roa.length; i++) {
roaKeys.push(Object.keys(roa[i]));
}
for(var j=0;j<roa.length;j++)
{
console.log(result[j].Code);
console.log(result[j].ElementName);
console.log(result[j].OptionName);
}
});
};
reader.readAsArrayBuffer(f);
}
}
//Change event to dropdownlist
$(document).ready(function(){
$('#files').change(handleFile);
});
</script>
</head>
<body>
<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" id="files" />
</body>
</html>
我无法独立访问行和列。请指导我如何做到这一点?