flutter - Exported Excel File is Blank When Using excel Package - Stack Overflow
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
1 Answer
Reset to default 0According 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.
- 谷歌回应欧盟反垄断指控:安卓利于竞争和消费者
- Windows 10盗版不易?所以在中国普及速度极慢
- 鲍尔默:未来5至10年微软将不再像一家软件公司
- 分析称苹果本届WWDC将侧重软件 不会发布iPhone 5
- Flutter iOS UrlLauncher EXC_BAD_ACCESS - Stack Overflow
- express - How can I access a cookie (refreshToken) on the server side in Next.js? - Stack Overflow
- asynchronous - Parallelization by threads vs Parallelization by processes on backend - Stack Overflow
- batch file - Command segment in .omp.json Oh-my-posh theme - Stack Overflow
- python - ModuleNotFoundError: No module named 'llama_index.text_splitter' - Stack Overflow
- python - Pyinstaller (Mac App) - Why only permission given to the executable file (instead of .app bundle) could work? - Stack O
- scala - How does maxOffsetsPerTrigger works in spark structured streaming - Stack Overflow
- React native - OCR of price tag - Stack Overflow
- tensorflow - How to resolved the import error with scipy when using keras_tuner? - Stack Overflow
- c++ - Member of struct constructed twice in custom constructor? - Stack Overflow
- swift - Infinite Update Loop with .onGeometryChange and Padding - Stack Overflow
- Excel VBA Overwrite Cells IF - Stack Overflow
- java - YubiKey PIV AuthenticationDecryption returns 0x6A80 error - Stack Overflow