How to create an Excel file in Dart
Build an .xlsx workbook from nothing and write it out, with no template file and no Office install.
Install#
dart pub add excel_plusimport 'package:excel_plus/excel_plus.dart';A minimal workbook#
final excel = Excel.createExcel();
final sheet = excel['Sheet1'];
sheet.updateCell(CellIndex.indexByString('A1'), TextCellValue('Hello, world!'));
final bytes = excel.save();Writing each value type#
Wrap the Dart value in the matching CellValue. Storing a real date rather than a string is what lets Excel sort and filter it correctly.
sheet.updateCell(CellIndex.indexByString('A1'), TextCellValue('Name'));
sheet.updateCell(CellIndex.indexByString('B1'), IntCellValue(42));
sheet.updateCell(CellIndex.indexByString('C1'), DoubleCellValue(3.14));
sheet.updateCell(CellIndex.indexByString('D1'), BoolCellValue(true));
sheet.updateCell(CellIndex.indexByString('E1'), DateCellValue(year: 2026, month: 6, day: 9));
sheet.updateCell(CellIndex.indexByString('F1'), TimeCellValue(hour: 9, minute: 30, second: 0));
sheet.updateCell(
CellIndex.indexByString('G1'),
DateTimeCellValue(year: 2026, month: 6, day: 9, hour: 9, minute: 30),
);Appending rows#
For tabular output you rarely want to compute addresses by hand. appendRow writes after the last filled row.
final sheet = excel['Sheet1'];
sheet.appendRow([TextCellValue('Product'), TextCellValue('Qty'), TextCellValue('Price')]);
for (final item in items) {
sheet.appendRow([
TextCellValue(item.name),
IntCellValue(item.quantity),
DoubleCellValue(item.price),
]);
}Several sheets#
final excel = Excel.createExcel();
excel['Summary'].updateCell(CellIndex.indexByString('A1'), TextCellValue('Total'));
excel['Detail'].updateCell(CellIndex.indexByString('A1'), TextCellValue('Line items'));
excel.rename('Sheet1', 'Overview');
excel.delete('Detail');Saving#
Pick whichever output the surrounding program needs.
// Bytes, for sending over the network or writing yourself.
final List<int>? bytes = excel.save();
// Straight to a file.
File('output.xlsx').writeAsBytesSync(excel.save()!);
// Trigger a download in the browser.
excel.save(fileName: 'report.xlsx');
// Encode off the main thread so a UI stays responsive.
final bytes = await excel.encodeAsync();For a workbook too big to hold in memory, stream it out instead. See large files.