Search code examples
angularxlsx

XLSX library with Angular


I'm using XLSX library to turn, in an Angular application, a HTML table into a XLS file.

I have no problem doing it, using the method table_to_sheet:

XLSX.utils.table_to_sheet(HTMLTableElement);

The problem is that I want, in the generated xls file, the data not starting from the first row (A1), but for example, from the tenth row (A10); this because I have to add some rows of text 'programmatically' before showing the data retrieved from the HTML table.

I read documentation but I didn't find a solution; I don't even know if it's possible.


Solution

  • According to the docs.

    There should be a function XLSX.utils.sheet_add_table(HTMLTableElement).

    First create your sheet with

    var sheet = XLSX.utils.table_to_sheet(HTMLTableElement table) 
    

    Then add your data at any point by specifying the origin in options parameter.

    XLSX.utils.sheet_add_dom(sheet , [[1,2], [2,3], [3,4]], {origin: "A2"});
    

    or

    XLSX.utils.sheet_add_dom(sheet , [[5,6,7], [6,7,8], [7,8,9]], {origin:{r:1, c:4}});