Articles in this section
Category / Section

How to export the Flutter calendar appointments into Excel.

1 min read

In the Flutter Calendar, you can export calendar appointments to an Excel document by using the Syncfusion® xlsio package.

STEP 1: Install the required packages.

syncfusion_flutter_calendar: 19.2.62
syncfusion_flutter_xlsio: ^19.2.62-beta
path_provider: ^2.0.1
open_file: ^3.2.1

STEP 2: Initialize the default values in initState

late AppointmentDataSource _dataSource;
@override
void initState() {
  _dataSource = AppointmentDataSource(_getAppointments());
  super.initState();
}

STEP 3: Use the Workbook and WorkSheet classes  to create an excel document based on the appointment data. Please find the code snippet for the web platform.

leading: IconButton(
  icon: const Icon(Icons.import_export),
  onPressed: () async {
    final Workbook workbook = Workbook();
    final Worksheet sheet = workbook.worksheets[0];
    sheet.getRangeByIndex(1, 1).setText('Id');
    sheet.getRangeByIndex(1, 2).setText('Subject');
    sheet.getRangeByIndex(1, 3).setText('Start Time');
    sheet.getRangeByIndex(1, 4).setText('End Time');
    int row = 2;
    for(int i = 0;i<_dataSource.appointments!.length;i++){
      int column = 1;
      final Appointment appointment = _dataSource.appointments![i];
      sheet.getRangeByIndex(row, column).setValue(i+1);
      column++;
      sheet.getRangeByIndex(row, column).setText(appointment.subject);
      column++;
      sheet.getRangeByIndex(row, column).setDateTime(appointment.startTime);
      column++;
      sheet.getRangeByIndex(row, column).setDateTime(appointment.endTime);
      row++;
    }
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
 
    AnchorElement(
        href:
        "data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
      ..setAttribute("download", "output.xlsx")
      ..click();
 
 
    // final directory = await getExternalStorageDirectory();
    //
    // final path = directory!.path;
    //
    // File file = File('$path/Output.xlsx');
    //
    // await file.writeAsBytes(bytes, flush: true);
    //
    // OpenFile.open('$path/Output.xlsx');
  },
),

View the GitHub sample here

Flutter Calendar Excel data

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied