Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, JQuery JQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. Learn More, JavaScript Javascript is an object-oriented computer programming language commonly used to create interactive effects within web browsers. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, CSV A CSV is a comma-separated values file, which allows data to be saved in a tabular format. CSVs look like a garden-variety spreadsheet but with a . csv extension. CSV files can be used with most any spreadsheet program, such as Microsoft Excel or Google Spreadsheets. Learn More, Microsoft Excel Microsoft Excel is a spreadsheet developed by Microsoft for Windows, macOS, Android and iOS. It features calculation, graphing tools, pivot tables, and a macro programming language called Visual Basic for Applications. Learn More,

convert my json object to csv downloadable file using jquery or javascript?

Description:I want to convert my json object to csv downloadable file for excel, I am having a simple list of submitted forms and the data is coming in the form of json against a ajax request on a controller method in C# ,that is why i want to convert my json on client side through jquery or javascipt.

Posted by: | Posted on: Dec 12, 2019

1 answers

Replies

2

I have also gone through the similar requirements and I have used following code which is working good enough for me.
You can place the following code in a separate js file and link that file to your page.

/**
@namespace Converts JSON to CSV.
Compress with: http://jscompress.com/
*/
(function (window) {
"use strict";
/**
Default constructor
*/
var _CSV = function (JSONData) {
if (typeof JSONData === 'undefined')
return;

var csvData = typeof JSONData != 'object' ? JSON.parse(settings.JSONData) : JSONData,
csvHeaders,
csvEncoding = 'data:text/csv;charset=utf-8,',
csvOutput = "",
csvRows = [],
BREAK = '\r\n',
DELIMITER = ',',
FILENAME = "export.csv";

// Get and Write the headers
csvHeaders = Object.keys(csvData[0]);
csvOutput += csvHeaders.join(',') + BREAK;

for (var i = 0; i < csvData.length; i++) {
var rowElements = [];
for(var k = 0; k < csvHeaders.length; k++) {
rowElements.push(csvData[i][csvHeaders[k]]);
} // Write the row array based on the headers
csvRows.push(rowElements.join(DELIMITER));
}
csvOutput += csvRows.join(BREAK);
// Initiate Download
var a = document.createElement("a");

if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([csvOutput], { type: "text/csv" }), FILENAME);
} else if ('download' in a) { //html5 A[download]
a.href = csvEncoding + encodeURIComponent(csvOutput);
a.download = FILENAME;
document.body.appendChild(a);
setTimeout(function() {
a.click();
document.body.removeChild(a);
}, 66);
} else if (document.execCommand) { // Other version of IE
var oWin = window.open("about:blank", "_blank");
oWin.document.write(csvOutput);
oWin.document.close();
oWin.document.execCommand('SaveAs', true, FILENAME);
oWin.close();
} else {
alert("Support for your specific browser hasn't been created yet, please check back later.");
}
};
window.CSVExport = _CSV;
})(window);

Then for the utilization of above code you can just pass your json in the object as shown in the following code

//you can fetch your json in the following variable from ajax request,then just need to pass it in the CSVExport as shown
var yourjson = [{Name: "umer", Id: 1},
{Name: "khalid", Id: 2},
{Name: "etc", Id: 3}];

var x = new CSVExport(yourjson);

Replied by: | Replied on: Dec 13, 2019



Reply