How to edit an existing Excel file
Open a workbook you already have, change what you need, and write it back with everything else untouched.
Install#
dart pub add excel_plusimport 'package:excel_plus/excel_plus.dart';Open, change, save#
The usual template workflow. Read the file, update the cells that matter, save it under a new name.
import 'dart:io';
import 'package:excel_plus/excel_plus.dart';
void main() {
final excel = Excel.decodeBytes(File('template.xlsx').readAsBytesSync());
final sheet = excel['Sheet1'];
sheet.updateCell(CellIndex.indexByString('B2'), TextCellValue('Updated'));
sheet.updateCell(CellIndex.indexByString('B3'), IntCellValue(2026));
File('output.xlsx').writeAsBytesSync(excel.save()!);
}Parts of the workbook that are not modelled, such as embedded images or printer settings, are carried across to the saved file byte for byte, so opening and saving does not quietly strip them.
Keeping a cell's existing style#
Writing a value replaces the cell. To change only the text and keep the formatting the template already had, pass the old style back in.
final index = CellIndex.indexByString('B2');
final existing = sheet.cell(index).cellStyle;
sheet.updateCell(index, TextCellValue('Updated'), cellStyle: existing);Rows and columns#
sheet.insertRow(2);
sheet.removeRow(5);
sheet.insertColumn(1);
sheet.removeColumn(3);Merging#
sheet.merge(
CellIndex.indexByString('A1'),
CellIndex.indexByString('D1'),
customValue: TextCellValue('Merged title'),
);
sheet.unMerge('A1:D1');Find and replace#
sheet.findAndReplace('draft', 'final');Column width and row height#
sheet.setColumnWidth(0, 24);
sheet.setRowHeight(0, 28);
sheet.setColumnAutoFit(1);