Read local xls/xlsx file in javascript

微笑、不失礼 提交于 2019-11-29 00:09:25
Reena

While looking for this I found this:

Basic parsers implemented in pure JS:

http://oss.sheetjs.com/js-xls/ (XLS files, what you wanted)

http://oss.sheetjs.com/js-xlsx/ (XLSX/XLSM/XLSB files)

I'm providing the reference How to parse Excel file in Javascript/HTML5

https://gist.github.com/psjinx/8002786

Hope this could be helpful.

use this code to read

 <script>
/* set up XMLHttpRequest */
var url = "test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
  var arraybuffer = oReq.response;

  /* convert data to binary string */
  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("");

  /* Call XLSX */
  var workbook = XLSX.read(bstr, {type:"binary"});

  /* DO SOMETHING WITH workbook HERE */
  var first_sheet_name = workbook.SheetNames[0];
  /* Get worksheet */
  var worksheet = workbook.Sheets[first_sheet_name];
  console.log(XLSX.utils.sheet_to_json(worksheet,{raw:true}));
}

oReq.send();
    </script>

https://github.com/SheetJS/js-xlsx is better liabrary at this time. But in my way files xlsx - are not reading, and files older then 2003 version of Excel also has trouble. But in official docs write then it is supported. But maybe just I have this trouble. I also use this feature for get json from excel:

var roa = XLSX.utils.sheet_to_row_object_array(worksheet);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!