2

I have been struggling with Javascript array data to put it in excel on the client side. I have read this,How to export JavaScript array info to csv (on client side)?

But that is for CSV. I want Excel.

1 Answer 1

5

I got a good answer. It may help someone out there

function excelformat() {
        var result_table = [
            ["Day", "Month", "Year"],
            ["1", "January", "2016"],
            ["2", "February", "2016"],
            ["3", "March", "2016"],
            ["4", "April", "2016"],
        ];
        var lineArray = [];
        result_table.forEach(function(infoArray, index) {
            var line = infoArray.join(" \t");
            lineArray.push(index == 0 ? line : line);
        });
        var csvContent = lineArray.join("\r\n");
        var excel_file = document.createElement('a');
        excel_file.setAttribute('href', 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(csvContent));
        excel_file.setAttribute('download', 'Visitor_History.xls');
        document.body.appendChild(excel_file);
        excel_file.click();
        document.body.removeChild(excel_file);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

that's not an excel file at all - it's still a CSV, with a .xls file extension
CSV comes with "," for rows and "\n" for new lines then datatype for csv is text/csv;charset=utf-8.
my point is you claim this creates an XLS file, it does not, it creates a CSV file with .xls as the filename extension - this doesn't change the actual content from csv to the far more complex xls
How can I do that please? For instance bold some lines, draw border lines.
it's your answer, dude
|

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.