Articles in this section
Category / Section

How to load the excel data to the Flutter Calendar?

3 mins read

In the Flutter Calendar, load the Excel sheet data into the calendar events.

STEP 1: Add the required packages in the Pubspec.yaml file.

syncfusion_flutter_calendar: 19.x.xx
intl: ^0.17.0
excel: ^1.1.5

STEP 2: Add the appointment data to the excel sheet and attach the excel file under the asset folder.

Excel sheet data

STEP 3: Initialize the default values in the initState().

List<Color> _colorCollection = <Color>[];
 
@override
void initState() {
  _initializeEventColor();
  super.initState();
}

STEP 4: Add the getDataFromExcel() method and using this method get the excel sheet data into the calendar events.

Future<List<Meeting>> getDataFromExcel() async {
  ByteData data = await rootBundle.load("assets/Schedule.xlsx");
  Uint8List bytes =
  data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  Excel excel = Excel.decodeBytes(bytes);
  int j = 0;
  final List<Meeting> appointmentData = [];
 
  for (String table in excel.tables.keys) {
    Map<int, List<dynamic>> mp = Map<int, List<dynamic>>();
    for (List<dynamic> row in excel.tables[table].rows) mp[j++] = row;
 
    int subjectIndex, startTimeIndex, endTimeIndex;
    if (mp != null) {
      for (int i = 0; i < mp.length; i++) {
        if (i == 0) {
          subjectIndex = mp[i].indexWhere((element) => element == 'Subject');
          startTimeIndex =
              mp[i].indexWhere((element) => element == 'StartTime');
          endTimeIndex = mp[i].indexWhere((element) => element == 'EndTime');
        } else {
          final Random random = new Random();
          Meeting meeting=Meeting.map(
              mp[i],
              _colorCollection[random.nextInt(9)],
              subjectIndex,
              startTimeIndex,
              endTimeIndex);
          appointmentData.add(meeting);
        }
 
      }
    }
  }
  return appointmentData;
}
 
static Meeting map(List<dynamic> data, Color color, int subjectIndex,
    int startTimeIndex, int endTimeIndex) {
  return Meeting(
      eventName: data[subjectIndex],
      from: DateFormat('dd/MM/yyyy HH:mm a').parse(data[startTimeIndex]),
      to: DateFormat('dd/MM/yyyy HH:mm a').parse(data[endTimeIndex]),
      background: color);
}

STEP 5: Assign those events into the dataSource property of the Flutter Calendar.

child: FutureBuilder(
  future: getDataFromExcel(),
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    if (snapshot.data != null) {
      return SafeArea(
        child: Container(
            child: SfCalendar(
              view: CalendarView.month,
              initialDisplayDate: DateTime(2020, 05, 01, 09, 0, 0),
              dataSource: MeetingDataSource(snapshot.data),
              monthViewSettings: MonthViewSettings(showAgenda: true),
            )),
      );
    } else {
      return Container(
        child: Center(
          child: Text('Loading'),
        ),
      );
    }
  },
),

View sample in GitHub

Flutter Calendar with Excel data

 

Conclusion

I hope you enjoyed learning how to load the excel data to the Flutter Calendar.

You can refer to our Flutter Calendar feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Flutter Calendar example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


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