1

I am struggling to get my code to display in HTML. I know I am probably missing something really simple, but I am stuck.

I have an Angular 7 app. I want to retrieve the xlsx data (worksheet) and display it in HTML. Simple right? My issue is that all the examples are in browser js and I need to retrieve it from a component. Here's the kicker, I already have the method and can print out json and html via console. log, but cannot find how to return it to an editable grid in HTML.

component.ts

fileUpload() {
let fileReader = new FileReader();
fileReader.onload = (e) => {
  this.arrayBuffer = fileReader.result;
  var data = new Uint8Array(this.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 first_sheet_name = workbook.SheetNames[0];
  var worksheet = workbook.Sheets[first_sheet_name];
  console.log(XLSX.utils.sheet_to_json(worksheet, { raw: true }));
  console.log(XLSX.utils.sheet_to_html(worksheet));

}
fileReader.readAsArrayBuffer(this.file);

}

html

<input type="file" style="display: inline-block;" (change)="incomingfile($event)" placeholder="Upload file" accept=".xls,.xlsx,.ods">

Upload

Currently I can retrieve any workbook/worksheet, but I simply cannot find how to return it to a ui grid of some sort from the .ts file.

I have been through numerous tutorials including ui-grid, ng-grid and https://redstapler.co/sheetjs-tutorial-convert-excel-html-table/

I appreciate I am probably being a dumbass, but any help would be appreciated. I cannot use a commercial component and simply need to render this in an editable table on my html page. I will then look at changing column headers and exporting the file via the backend SpringBoot REST to Mongo (all working)

I am aware that there are numerous similar questions on SO, but the working examples do not seem to display the data(even without any errors). Could be a versioning error etc. I also tried ui-grid, but came up with this issue: 'readAsArrayBuffer' on 'FileReader': parameter 1 is not of type 'Blob'. The example here is however also in .js and not via a typescript component: parameter is not of type 'Blob'

I am simply not able to bind the output to the HTML

  • Hi all, further to this I have tried this demo https://github.com/SheetJS/js-xlsx/tree/master/demos/angular2, but still cannot get the output to show in the browser. This article only covers to angular 5, but still no joy. – LittleBuddha Dec 17 '18 at 08:46
  • ok so now I have gone full circle with https://stackoverflow.com/questions/47151035/angular-4-how-to-read-data-from-excel and I still cannot get the data to display? Perhaps it is an Angular 7 issue? – LittleBuddha Dec 17 '18 at 10:52
  • OK!!! A little success, I can now return the excel as a basic html string, but it's not pretty. Could anyone recommend an OSS widget or angular table that will "prettify" the returned json array of arrays. – LittleBuddha Dec 17 '18 at 15:37
  • Hi there, did you ever find a resolution to this? I am looking for the same thing. – Anubhav Oct 03 '20 at 20:34
  • Hi @Anubhav I eventually just displayed it manually. – LittleBuddha Oct 05 '20 at 23:43

0 Answers0