我有一个JS代码片段,该片段显示HTML上的XLSX文件内容,使用浏览按钮选择文件,然后上传以显示XLSX内容,我试图使其自动化以保持每30秒刷新一次页面再次从Excel中提取数据。
尝试使用setinterval,但是没有用。 (由于它不会再次重新上传文件,因此什么也没有发生,我必须打开页面并重新选择文件,然后再次按上载)。
这是到目前为止的代码
<html>
<body>
<input type="file" id="fileUpload" />
<input type="button" id="upload" value="Upload" onclick="Upload()" />
<hr />
<div id="dvExcel">
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/xlsx.full.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.13.5/jszip.js"></script>
<script type="text/javascript">
function Upload() {
//Reference the FileUpload element.
var fileUpload = document.getElementById("fileUpload");
var newtime = 3000
//Validate whether File is valid Excel file.
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xls|.xlsx)$/;
if (regex.test(fileUpload.value.toLowerCase())) {
if (typeof (FileReader) != "undefined") {
var reader = new FileReader();
//For Browsers other than IE.
if (reader.readAsBinaryString) {
reader.onload = function (e) {
ProcessExcel(e.target.result);
};
reader.readAsBinaryString(fileUpload.files[0]);
} else {
//For IE Browser.
reader.onload = function (e) {
var data = "";
var bytes = new Uint8Array(e.target.result);
for (var i = 0; i < bytes.byteLength; i++) {
data += String.fromCharCode(bytes[i]);
}
ProcessExcel(data);
};
reader.readAsArrayBuffer(fileUpload.files[0]);
}
} else {
alert("This browser does not support HTML5.");
}
} else {
alert("Please upload a valid Excel file.");
}
};
function ProcessExcel(data) {
//Read the Excel File data.
var workbook = XLSX.read(data, {
type: 'binary'
});
//Fetch the name of First Sheet.
var firstSheet = workbook.SheetNames[0];
//Read all rows from First Sheet into an JSON array.
var excelRows = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[firstSheet]);
//Create a HTML Table element.
var table = document.createElement("table");
table.border = "1";
//Add the header row.
var row = table.insertRow(-1);
//Add the header cells.
var headerCell = document.createElement("TH");
headerCell.innerHTML = "Name";
row.appendChild(headerCell);
headerCell = document.createElement("TH");
headerCell.innerHTML = "ChatStatus";
row.appendChild(headerCell);
headerCell = document.createElement("TH");
headerCell.innerHTML = "EmailStatus";
row.appendChild(headerCell);
headerCell = document.createElement("TH");
headerCell.innerHTML = "Duration";
row.appendChild(headerCell);
//Add the data rows from Excel file.
for (var i = 0; i < excelRows.length; i++) {
//Add the data row.
var row = table.insertRow(-1);
//Add the data cells.
var cell = row.insertCell(-1);
cell.innerHTML = excelRows[i].Name;
cell = row.insertCell(-1);
cell.innerHTML = excelRows[i].ChatStatus;
cell = row.insertCell(-1);
cell.innerHTML = excelRows[i].EmailStatus;
cell = row.insertCell(-1);
cell.innerHTML = excelRows[i].Duration;
}
var dvExcel = document.getElementById("dvExcel");
dvExcel.innerHTML = "";
dvExcel.appendChild(table);
};
var myVar = setInterval(ProcessExcel(), 3000);
</script>
</body>
</html>