2

I have an expandable table in html:

enter image description here

How can I convert it into excel file with grouping, something like this:

enter image description here

1 Answer 1

1

Im not to sure about the grouping in XLS but the main way we convert data into XL is to use CSV

You can parse the data from your HTML into an array then export that array to CSV

Here is the code to make your html table downloadable to CSV

var data = [
   ['Foo', 'programmer'],
   ['Bar', 'bus driver'],
   ['Moo', 'Reindeer Hunter']
];
 
 
function download_csv() {
    var csv = 'Name,Title\n';
    data.forEach(function(row) {
            csv += row.join(',');
            csv += "\n";
    });
 
    console.log(csv);
    var hiddenElement = document.createElement('a');
    hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
    hiddenElement.target = '_blank';
    hiddenElement.download = 'people.csv';
    hiddenElement.click();
}

 
<button onclick="download_csv()">Download CSV</button> 

Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the code, but the depth will be lost, am I wrong?
Well you could use and array in an array and it should still retain the depth
what do you mean with "and array"?
What format would you get if you exported the XLS with the depth to csv ... If that csv retains the depth then adjust the code above to ensure that there is depth retention so instead of download_csv() function using it as an array try use a JSON object that indicates the depth. The image you posted looks like hide columns in XLS.... I think what you are trying to do with depth is shift a column right with the depth .That would be easier to achieve as JSON
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.