[英]Parsing Excel sheet with js-xlsx
I am trying to parse all the excel files in a directory specified by the user but the js-xlsx library I am using seems to need manual navigation. 我试图解析用户指定的目录中的所有excel文件,但我使用的js-xlsx库似乎需要手动导航。
var url = "/test-files/test.xlsx"; <-------- Located in the project directory
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for (var i = 0; i != data.length; i++) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
var workbook = XLSX.read(bstr, {
type: "binary"
});
}
oReq.send();
The code needs to be dynamic in that it can open an excel file anywhere. 代码需要是动态的,因为它可以在任何地方打开excel文件。
Is there any way that I can use a fileentry object with js-xlsx library to parse an excel file? 有什么办法可以使用带有js-xlsx库的fileentry对象来解析excel文件吗?
For those who may be curious I figured out a solution. 对于那些可能很好奇的人,我想出了一个解决方案。
In order to dynamically create a path to a given fileEntry object you must first convert it into a Blob: 为了动态创建给定fileEntry对象的路径,必须先将其转换为Blob:
fileEntryObject.file(function(file) {});
Then convert it to a window.URL that way your project may have access to a readable path to the desired file: 然后将其转换为window.URL ,这样您的项目可以访问所需文件的可读路径:
var newPath = window.URL.createObjectURL(file);
Thus you may use it like a regular path in your functions even if you don't know how to navigate from your project to the file: 因此,即使您不知道如何从项目导航到文件,也可以像在函数中使用常规路径一样使用它:
fileEntryObject.file(function(file) {
var newPath = window.URL.createObjectURL(file);
var oReq = new XMLHttpRequest();
oReq.open("GET", newPath, true);
oReq.responseType = "arraybuffer";
oReq.onError = function(e) {
console.log('Error in reading excel file');
};
oReq.onload = function(e) {
var arraybuffer = oReq.response;
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
var bstr = arr.join("");
var workbook = XLSX.read(bstr, {
type: "binary"
});
var sheet_name = workbook.SheetNames[1];
var worksheet = workbook.Sheets[sheet_name];
self.parseReceive(worksheet, callback);
// self.parseReceive(worksheet);
};
oReq.send();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.