how to generate Excel through Javascript

Is there any way to generate Excel/CSV through Javascript? (It should be browser compaatible too)


You can generate whatever you like, but the question is - how are you going to present it to the user? If your JS runs in the browser, you won't be able to make a save-as dialog box or even open it in the same window. Not unless you use some ActiveX controls, but that's an IE-only solution which will also give the user a few confirmation prompts before it starts to work.

There is an interesting project on github called Excel Builder (.js) that offers a client-side way of downloading Excel xlsx files and includes options for formatting the Excel spreadsheet.

You may encounter both browser and Excel compatibility issues using this library, but under the right conditions, it may be quite useful.

The demos seem to hold some promise.

Another github project with less Excel options but less worries about Excel compatibility issues can be found here: ExcellentExport.js

If you are using AngularJS, there is ng-csv: a "Simple directive that turns arrays and objects into downloadable CSV files".

If you can generate the Excel file on the server, that is probably the best way. With Excel you can add formatting and get the output to look better. Several Excel options have already been mentioned. If you have a PHP backend, you might consider phpExcel.

If you are trying to do everything on the client in javascript, I don't think Excel is an option. You could create a CSV file and create a data URL to allow the user to download it.

I created a JSFiddle to demonstrate:

This javascript (assuming you are using jQuery) will take the values out of input boxes in a table and build a CSV formatted string:

var csv = "";
$("table").find("tr").each(function () {
    var sep = "";
    $(this).find("input").each(function () {
        csv += sep + $(this).val();
        sep = ",";
    csv += "\n";

If you wish, you can drop the data into a tag on the page (in my case a tag with an id of "csv"):


You can generate a URL to that text with this code:

window.URL = window.URL || window.webkiURL;
var blob = new Blob([csv]);
var blobURL = window.URL.createObjectURL(blob);

Finally, this will add a link to download that data:

attr("href", blobURL).
attr("download", "data.csv").
text("Download Data").

Create an AJAX postback method which writes a CSV file to your webserver and returns the url.. Set a hidden IFrame in the browser to the location of the CSV file on the server.

Your user will then be presented with the CSV download link.

I recommend you to generate an open format XML Excel file, is much more flexible than CSV. Read for more info

To answer your question with a working example:

<script type="text/javascript">
function DownloadJSON2CSV(objArray)
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = new Array();

        for (var index in array[i]) {
           line.push('"' + array[i][index] + '"');

        str += line.join(';');
        str += '\r\n';
    } "data:text/csv;charset=utf-8," + encodeURIComponent(str));

Need Your Help

Replacing string in textfile with quotes and doublequotes with sed

string unix replace sed quotes

Lets say I want to replace the following string within a textfile:

C# Variable = new function () {};

c# function variables

Within C# is it possible to create a new function on the fly to define a variable?