我正在使用xlsx.core.min.js将excel文件上传到SP,并将其内容传递到新列表。我可以在Edge中使用它,但不能在IE 11中使用。
我收到的第一个控制台错误是“对象不支持属性或方法'charCodeAt'”
我阅读了有关.readAsArrayBuffer()的兼容性问题并将其替换。然后,我开始收到错误“对象不支持属性或方法'readAsBinaryString'”
**注意:仅使用.readAsArrayBuffer()会导致原始的'charCodeAt'控制台错误**
如何使此代码正常工作?预先谢谢你!
function ExportToTable() {
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/;
/*Checks whether the file is a valid excel file*/
if (regex.test($("#excelfile").val().toLowerCase())) {
var xlsxflag = false;
/*Flag for checking whether excel is .xls format or .xlsx format*/
if ($("#excelfile").val().toLowerCase().indexOf(".xlsx") > 0) {
xlsxflag = true;
}
/*Checks whether the browser supports HTML5*/
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
reader.onload = function (e) {
var data = e.target.result;
/*Converts the excel data in to object*/
if (xlsxflag) {
var workbook = XLSX.read(data, { type: 'binary' });
}
else {
var workbook = XLS.read(data, { type: 'binary' });
}
/*Gets all the sheetnames of excel in to a variable*/
var sheet_name_list = workbook.SheetNames;
var cnt = 0; /*This is used for restricting the script to consider only first sheet of excel*/
sheet_name_list.forEach(function (y) { /*Iterate through all sheets*/
/*Convert the cell value to Json*/
if (xlsxflag) {
var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
}
else {
var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);
}
//alert (exceljson.length + " ----" + cnt);
if (exceljson.length > 0 && cnt == 0) {
BindTable(exceljson, '#exceltable');
cnt++;
}
});
$('#exceltable').show();
}
if (xlsxflag) {/*If excel file is .xlsx extension than creates a Array Buffer from excel*/
reader.readAsArrayBuffer($("#excelfile")[0].files[0]);
}
else {
reader.readAsBinaryString($("#excelfile")[0].files[0]);
}
}
else {
// alert("Sorry! Your browser does not support HTML5!");
}
}
else {
// alert("Please upload a valid Excel file!");
}
}
答案 0 :(得分:0)
尝试参考以下代码来读取 .xlsx文件。
document.getElementById('file-object').addEventListener("change", function(e) {
var files = e.target.files,file;
if (!files || files.length == 0) return;
file = files[0];
var fileReader = new FileReader();
fileReader.onload = function (e) {
var filename = file.name;
// pre-process data
var binary = "";
var bytes = new Uint8Array(e.target.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
// call 'xlsx' to read the file
var oFile = XLSX.read(binary, {type: 'binary', cellDates:true, cellStyles:true});
};
fileReader.readAsArrayBuffer(file);
});
更多详细信息,请检查this article。