I want to convert .json file to excel . iam not able to find the solution anywhere for these issue using c# language . Could any one help me out with these along with exact solution.
-
First deserialize your json using a library(JSON.NET) and then iterate your data and create the excel rows. msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx If you have any problems post the code here and we will helpGeorge Vovos– George Vovos2014-09-07 08:57:52 +00:00Commented Sep 7, 2014 at 8:57
-
2@GeorgeVovos Using interop is problematic (requires Office installed and can't be used on webpages due to licensing), good alternative is OpenXML or its wrapper - ClosedXML.PTwr– PTwr2014-09-07 09:08:25 +00:00Commented Sep 7, 2014 at 9:08
-
PTwr is right.When you do it, don't use interop.I also had trouble with it beforeGeorge Vovos– George Vovos2014-09-07 09:12:19 +00:00Commented Sep 7, 2014 at 9:12
-
Thanks a lot . i will try these procedure as u all said.user3628181– user36281812014-09-07 09:27:57 +00:00Commented Sep 7, 2014 at 9:27
4 Answers
Here is another partial solution, that doesn't require making a json definition, as long as the json is in a table format.
DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
From here, you can use any of the numerous datatable -> excel solutions available on the internet.
Many are posted here: How to export DataTable to Excel
Comments
It depends on the data and what you want to achieve on the Excel side. I would tackle this with Json.NET and output to a CSV file, which may be opened in Excel.
Consider the following JSON... a simple array
[{
"foo": "bar"
}]
With Json.NET we can do the following to get it into C#
var jsonData = "[{ \"foo\": \"bar\" }]";
var jsonDefinition = new object[]
{
new {foo = ""}
};
var result = JsonConvert.DeserializeAnonymousType(jsonData, jsonDefinition);
We can then loop through the structure, outputting to a CSV file.
var sb = new StringBuilder();
foreach (dynamic o in result)
{
sb.AppendLine(o.foo);
}
File.WriteAllText(@"c:\code\test.csv", sb.ToString());
3 Comments
It can be done with the help of EPPlus, check below code for complete answer.
Create And write Json Data to Excel
using (StreamReader r = new StreamReader(file)) { string json = r.ReadToEnd(); var obj = JsonConvert.DeserializeObject<JObject>(json); List<JsonData> dataList = GetDataList(obj); var fileName = file.Split('\\').Last().Split('.')[0].Trim().ToString(); using (ExcelPackage excel = new ExcelPackage()) { excel.Workbook.Worksheets.Add(fileName); var headerRow = new List<string[]>() { new string[] { "Key", "Value"} }; string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1"; var worksheet = excel.Workbook.Worksheets[fileName]; worksheet.Cells[headerRange].LoadFromArrays(headerRow); worksheet.Cells[2, 1].LoadFromCollection(dataList); FileInfo excelFile = new FileInfo($"D:\\JsonFilesToExcel\\{folder}\\{fileName}.xlsx"); excel.SaveAs(excelFile); } }Create C# Object from Reading Json Key Value pairs
public static List<JsonData> GetDataList(JObject obj) { var listResult = new List<JsonData>(); foreach (JProperty item in (JToken)obj) { listResult.Add(new JsonData { Key = item.Name, Value = item.Value.ToString() }); } return listResult; }
1 Comment
I used ExcelMapper. This app is great because I just have to send it a C# list and ExcelMapper converts it to Excel. Wow!! It is available via Nuget Manager.
ExcelMapper em = new ExcelMapper();
em.Save(stExportFileFullPath, items, "Totals", true);
/*
items is a list composed of the below class and fields.
public class CarrierRevenueLOBReport
{
public int Year { get; set; }
public int Month { get; set; }
public string Carrier { get; set; }
public string LOBName { get; set; }
public string NewOrRenewal { get; set; }
public double SumPremium { get; set; }
public double SumCommision { get; set; }
}
*/