Click here to Skip to main content
15,500,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using ASP.Net Core Web Api and writing the Api which return the data in nested form and its done the Api returning the data like i want now i want to export that data in excel sheet with EPPlus, here is data sample
[
  {
    "BIDFormCategoryId": "S1",
    "BIDFormCateogry": "Computer Monitor",
    "FirstLevel": [
      {
        "BIDFormId": "S1-27",
        "BIDFormComponent": "Inspiron27",
        "SecondLevel": [
          {
            "ObjectName": "Optiflex 27 inch high resolution monitor",
            "Quantity": 15000,
            "Length": 56,
            "Area": 85,
            "Volume": 1587
          }
        ]
      },
      {
        "BIDFormId": "S1-22",
        "BIDFormComponent": "Inspiron22",
        "SecondLevel": [
          {
            "ObjectName": "Optiflex 22 inch high resolution monitor",
            "Quantity": 1500,
            "Length": 45,
            "Area": 65,
            "Volume": 875
          }
        ]
      }
    ]
  },
  {
    "BIDFormCategoryId": "S2",
    "BIDFormCateogry": "Laptop Monitor",
    "FirstLevel": [
      {
        "BIDFormId": "S1-27",
        "BIDFormComponent": "Inspiron27",
        "SecondLevel": [
          {
            "ObjectName": "Optiflex 27 inch high resolution monitor",
            "Quantity": 15000,
            "Length": 56,
            "Area": 85,
            "Volume": 1587
          }
        ]
      },
      {
        "BIDFormId": "S1-22",
        "BIDFormComponent": "Inspiron22",
        "SecondLevel": [
          {
            "ObjectName": "Optiflex 22 inch high resolution monitor",
            "Quantity": 1500,
            "Length": 45,
            "Area": 65,
            "Volume": 875
          }
        ]
      }
    ]
  }


What I have tried:

C#
using(ExcelPackage excelPackage = new ExcelPackage())
   {
     excelPackage.Workbook.Properties.Author = "Samee";
     excelPackage.Workbook.Properties.Title = "Architecture Review";
     excelPackage.Workbook.Properties.Created = DateTime.Now;
     ExcelWorksheet excelWorksheet = excelPackage.Workbook.Worksheets.Add( "ARCHITECTURE" );
                    for(int i = 0; i < meta.Count + 1; i++)
                    {
                        excelWorksheet.Cells[1, i].Value = meta.ToString()[i];
                    }
                    for(int i = 0; i < meta.Count - 1; i++)
                    {
                        for(int j = 0; j < meta.Count; j++)
                        {
                            excelWorksheet.Cells[i + 2, j + 1].Value = meta.ToString()[j];
                        }
                    }
}
Posted
Updated 26-Jul-22 0:17am
v2
Comments
Maciej Los 26-Jul-22 14:47pm    
What is meta???
CHill60 27-Jul-22 3:39am    
What happens when you run your code?
matblue25 28-Jul-22 9:53am    
The first time through your “for(int i = 0;” loop, the attempt to set “excelWorksheet.Cells[1, i].Value” will fail because Excel rows and columns start at 1.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900