How to export json data / table data to excel sheet using flutter package

Viewed 143

This this the code i have written to create a basic excel sheet on click of a button. However i am not sure how to create an excel sheet from json data. The excel sheet must contain four columns which will be the keys and rows data which will be the values.

Future<void> createExcel() async 
{

    final excel = Excel.createExcel();

    final sheet = excel.sheets[excel.getDefaultSheet() as String];
    sheet!.setColWidth(2, 10);
    sheet.setColAutoFit(3);

    sheet.cell(CellIndex.indexByColumnRow(columnIndex: 2, rowIndex: 3)).value =
        'Name';

    sheet.cell(CellIndex.indexByColumnRow(columnIndex: 3, rowIndex: 4)).value =
        'obi wan kanobi';

    excel.save();
}

The json data is a s follows

{
 "results": [
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      },
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      },
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      },
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      },
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      },
      {
        "first_name": "Suprem",
        "last_name": "Nandal",
        "email": "suprem.nandal@gmail.com",
        "phone": "+911234567890"
      }
}```
0 Answers
Related