mounika ymr

mounika ymr

  • NA
  • 90
  • 32.9k

Convert Json data to Excel File in the Specific Format

Mar 12 2018 5:31 AM
I'm new to C# I've tried so many ways to tackle this scenario, but i couldn't find a way to achieve this task, I've a string data I wanted to Write into Excel file in a specific format. I've attached Excel Format and Json data in zip file. Please find the attachment. I want to split the string in such a way that it should be appear as given excel format Please Provide me a solution ASAP. Thanks in Advance. 
 
//Note: Strings with Same Column Name should write under that column and if that column is not present in the particular string it should write null
 
Code I've tried so far:
 
void StreamExcelFile(Stream Stream1)
{
//StreamWriter writer1 = new StreamWriter(Stream1);
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet1;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
var count = response.Where(x => x == ':').Count();
// response = response.Replace('"', ' ').Trim();
response = response.Replace('{', ' ').Trim();
response = response.Replace('}', ' ').Trim();
//response = response.Replace(':', ' ').Trim();
//response = response.Replace(',', ' ').Trim();
//response = response.Replace(" ", "").Trim();
int j = 1, k = 2;
for (int i = 1; i <= 60; i++)
{
// string[] coldata = response.Split(new char[] { ':' }, 2);
// string rowdata = response.Split(':', ',')[i];
string rowdata;
rowdata = response.Split(':', ',')[i];
//if(rowdata.Contains(""))
//{
// rowdata.Split(' ').Skip(1);
// i;
//}
if (i % 2 == 0)
{
if (i == 2)
xlWorkSheet1.Cells[2, i - 1].value = rowdata;
else
{
xlWorkSheet1.Cells[2, i - (1 * k)].value = rowdata;
k = k + 1;
}
}
else if (i == 1)
xlWorkSheet1.Cells[1, i].value = rowdata;
else
{
xlWorkSheet1.Cells[1, i - (1 * j)].value = rowdata;
j = j + 1;
}
}
Excel.Worksheet xlWorkSheet2;
xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.Add(System.Reflection.Missing.Value, xlWorkBook.Worksheets[xlWorkBook.Worksheets.Count], System.Reflection.Missing.Value, System.Reflection.Missing.Value);
if (System.IO.File.Exists("D:\\ExcellData.xls"))
{
File.Delete("D:\\ExcellData.xls");
}
xlWorkBook.SaveAs("D:\\ExcellData.xls");
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet1);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}

Answers (5)