skip to Main Content

I need to download excel file to device, the api return is List, the sample data is shown below.

{"data":[{"First Name":"1","Last Name":"Dulce","Gender":"Abril","Country":"Female","Age":"United States","Date":"32","Id":"15/10/2017"},{"First Name":"2","Last Name":"Mara","Gender":"Hashimoto","Country":"Female","Age":"Great Britain","Date":"25","Id":"16/08/2016"},{"First Name":"3","Last Name":"Philip","Gender":"Gent","Country":"Male","Age":"France","Date":"36","Id":"21/05/2015"},{"First Name":"4","Last Name":"Kathleen","Gender":"Hanner","Country":"Female","Age":"United States","Date":"25","Id":"15/10/2017"},{"First Name":"5","Last Name":"Nereida","Gender":"Magwood","Country":"Female","Age":"United States","Date":"58","Id":"16/08/2016"}]}

My question is how can i make excel file from this array. I have to create excel file and download it to phone (device).

2

Answers


  1. You can use this package excel

    import 'package:excel/excel.dart';
    
    
    class ExcelReportExtractor {
      final Excel excelFile = Excel.createExcel();
      create({required Map<String, dynamic> model}) {
        Map<String, dynamic> merged = {};
        merged.addEntries([MapEntry("Created at:", DateFormat('dd.MM.yyyy HH.mm').format(DateTime.now().toUtc()))]);
        merged.addEntries(model.entries);
    
        Sheet defaultSheet = excelFile[excelFile.getDefaultSheet()!];
        List.generate(merged.keys.length, (index) {
          if (index % 2 == 0) {
            defaultSheet.cell(CellIndex.indexByString(
              "A${(index + 1).toString()}",
            ))
              ..value = formatString(merged.keys.elementAt(index));
    
            defaultSheet.cell(CellIndex.indexByString(
              "B${(index + 1).toString()}",
            ))
              ..value = merged.values.elementAt(index)
              ..cellStyle = CellStyle(
                bold: true,
                textWrapping: TextWrapping.WrapText,
                fontFamily: getFontFamily(FontFamily.Arial),
                rotation: 0,
                horizontalAlign: HorizontalAlign.Right,
              );
          } else {
            defaultSheet.cell(CellIndex.indexByString(
              "A${(index + 1).toString()}",
            ))
              ..value = formatString(merged.keys.elementAt(index))
              ..cellStyle = CellStyle(backgroundColorHex: '#D7D7D7');
    
            defaultSheet.cell(CellIndex.indexByString(
              "B${(index + 1).toString()}",
            ))
              ..value = merged.values.elementAt(index)
              ..cellStyle = CellStyle(
                  bold: true, textWrapping: TextWrapping.WrapText, fontFamily: getFontFamily(FontFamily.Arial), rotation: 0, horizontalAlign: HorizontalAlign.Right, backgroundColorHex: '#D7D7D7');
          }
        });
    
        defaultSheet.setColAutoFit(0);
        defaultSheet.setColAutoFit(1);
    
        excelFile.save(fileName: "output.xlsx");
      }
    }
    
    Login or Signup to reply.
  2. CSV data can be generated without any extra dependencies, assuming it fulfils the requirements. Something like this:

    final data = jsonDecode(json)["data"];
    final buffer = StringBuffer()
      ..writeAll(data[0].keys, ",")
      ..writeln();
    for (final item in data) {
      buffer
        ..writeAll(item.values, ",")
        ..writeln();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search