PalFS
PalFS

Reputation: 831

open excel file with xlsx.js by path

Hi i want to open excel file with xlsx extension with xlsx.js library. I can open file by html input but i want to open it by using file path.

I have this code extracted from xlsx.js demo:

function handleFile(e) {
    rABS = false;
    use_worker = false;

    console.log(e);

    var files = e.target.files;


    var f = files[0]; {
        var reader = new FileReader();
        var name = f.name;
        reader.onload = function (e) {
            if (typeof console !== 'undefined')
                console.log("onload", new Date(), rABS, use_worker);

            var data = e.target.result;
            console.log("target result >>>>>>>>> " + e.target.result);
            if (use_worker) {
                xw(data, process_wb);
            } else {
                var wb;
                if (rABS) {
                    wb = X.read(data, {
                            type : 'binary'
                        });
                } else {
                    var arr = fixdata(data);
                    wb = X.read(btoa(arr), {
                            type : 'base64'
                        });
                }
                process_wb(wb);
            }
        };
        if (rABS)
            reader.readAsBinaryString(f);
        else
            reader.readAsArrayBuffer(f);
    }
}

I want something like this:

var file = new File([""],"C:\\Users\\PalFS\\Downloads\\Fiverr_Example_List (1).xlsx");

var data = file;

How to do this or how can i convert this file into arraybuffer like it is returned from, var data = e.target.result;.

Thanks

Upvotes: 0

Views: 5030

Answers (1)

guest271314
guest271314

Reputation: 1

You could use --allow-file-access-from-files flag at chrome, chromium with XMLHttpRequest , .responseType set to "arraybuffer" to retrieve .xlsx file from local filesystem as ArrayBuffer; set new File() data to returned ArrayBuffer. Second parameter at new File() constructor should set the .name property of created file.

Launch /path/to/chrome --allow-file-access-from-files

  var request = new XMLHttpRequest();
  // may be necessary to escape path string?
  request.open("GET", "C:\\Users\\PalFS\\Downloads\\Fiverr_Example_List (1).xlsx");
  request.responseType = "arraybuffer";
  request.onload = function () {
     // this.response should be `ArrayBuffer` of `.xlsx` file
     var file = new File(this.response, "Fiverr_Example_List.xlsx");
     // do stuff with `file`
  };
  request.send();

Upvotes: 1

Related Questions