flutter - Exported Excel File is Blank When Using excel Package - Stack Overflow

时间: 2025-01-06 admin 业界

I am trying to export data to an Excel file in a Flutter app using the excel package. The file is created successfully and can be opened, but the sheet is completely blank (no data or headers).

Here’s the relevant part of my code for generating and exporting the Excel file:

  Future<void> _exportToExcel(List<Map<String, dynamic>> feedRecords) async {
  try {
    final excel = Excel.createExcel();
    final sheet = excel['Feed Records'];

    // Adding header row
    sheet.appendRow(['Date', 'Brand', 'Size', 'Bags']);

    // Adding data rows
    for (var record in feedRecords) {
      sheet.appendRow([
        record['date'].toString(),
        record['brand'].toString(),
        record['size'].toString(),
        record['bags'].toString(),
      ]);
    }

    // Save file
    final directory = await getExternalStorageDirectory();
    final fileName = 'FeedRecords.xlsx';
    final filePath = '${directory?.path}/$fileName';
    final fileBytes = excel.save();

    if (fileBytes != null) {
      final file = File(filePath)
        ..createSync(recursive: true)
        ..writeAsBytesSync(fileBytes);
      print('File saved at $filePath');
    }
  } catch (e) {
    print('Error exporting to Excel: $e');
  }
}

I am trying to export data to an Excel file in a Flutter app using the excel package. The file is created successfully and can be opened, but the sheet is completely blank (no data or headers).

Here’s the relevant part of my code for generating and exporting the Excel file:

  Future<void> _exportToExcel(List<Map<String, dynamic>> feedRecords) async {
  try {
    final excel = Excel.createExcel();
    final sheet = excel['Feed Records'];

    // Adding header row
    sheet.appendRow(['Date', 'Brand', 'Size', 'Bags']);

    // Adding data rows
    for (var record in feedRecords) {
      sheet.appendRow([
        record['date'].toString(),
        record['brand'].toString(),
        record['size'].toString(),
        record['bags'].toString(),
      ]);
    }

    // Save file
    final directory = await getExternalStorageDirectory();
    final fileName = 'FeedRecords.xlsx';
    final filePath = '${directory?.path}/$fileName';
    final fileBytes = excel.save();

    if (fileBytes != null) {
      final file = File(filePath)
        ..createSync(recursive: true)
        ..writeAsBytesSync(fileBytes);
      print('File saved at $filePath');
    }
  } catch (e) {
    print('Error exporting to Excel: $e');
  }
}
Share Improve this question edited yesterday Ken White 126k15 gold badges233 silver badges463 bronze badges asked yesterday Richie_ElohRichie_Eloh 458 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

According to the package documentation the appendRow function does not accept string values in the array but TextCellValue(), DateCellValue() and similar.

Instead of this:

sheet.appendRow(['Date', 'Brand', 'Size', 'Bags']);

try this:

sheet.appendRow([TextCellValue('Date'), TextCellValue('Brand'), TextCellValue('Size'), TextCellValue('Bags')]);

In order to add your records you might need other class listed here depending on the type of your data fields.