1

I'm a newbie to AngularJS.

I've an excel file titled sample.xls already present on my server in some folder. This excel file contains four sheets in it. I want to parse this entire file(i.e. data present in all the sheets).

How should I do this in AngularJS?

I don't want the code to upload an excel file to server in AngularJS. I want a way to parse the excel file which is already present on server using AngularJS.

Please somebody help me in this regard.

It would be better if you could take some excel file with multiple sheets contained in it and demonstrate your code with some working example.

Thanks.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • refer this link http://stackoverflow.com/questions/29465930/read-local-xls-xlsx-file-in-javascript – Riaz Mar 08 '16 at 14:27
  • @Riazaskather:I checked this. I want this to be done using AngularJS only. – PHPLover Mar 08 '16 at 14:28
  • Can you specify what is your goal? Unless there a really special case, there is no reason not to parse the Excel file on the server-side and wire your relevant data to the client. – Alexei - check Codidact Jan 11 '17 at 19:30

3 Answers3

0

You could request the excel file from the server and then use https://github.com/SheetJS/js-xlsx to process the results in Javascript/Typescript.

Although personally, I would opt to do the reading server side in language of choice and then return the contained data as a Json object.

Tim Almond
  • 12,088
  • 10
  • 40
  • 50
0

use this code to read data from excel file

 <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>
Vinoth
  • 972
  • 1
  • 16
  • 47
0

I know this is an old question, but this answer could help others.

To get the XLS file from the server using AngularJS:

var handler = function(response) {
  // response.data will have your xls file as an arraybuffer

  // then do as the other answers have suggested
  /* convert data to binary string */
  var data = new Uint8Array(response.data);
  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}));
}
// get the XLS file from the server based on the url you pass
$http.get(url, {'responseType':'arraybuffer'}).then(handler);
JM-AGMS
  • 1,670
  • 1
  • 15
  • 33