Read excel file (.xlsx) in Flutter


 

In many scenarios you would want to read an excel file(.xlsx) in flutter because excel files are considered efficient when storing data. You may want to display data of a excel file directly in your app..Especially excel file stores data in tabular form as stored in an SQLite database. So one scenario is when you are using SQLite database in flutter and want to insert data in the database from an excel file.

Note : If you want to learn how to properly and efficiently use SQLite in Flutter, read this article.

So let's learn in this tutorial how we can read an excel file in Flutter.

For this  tutorial we will be using an excel file Animals.xlsx containing names of different animals.

 

 

 1. Add your excel file in the assets folder of the project directory.

Note : assets directory should be in the same level as lib directory. 




 2. If assets folder is not added in pubspec.yaml then first add it in pubspec.yaml


  flutter:
      assets:
        - assets/

 

 Note : Don't forget to do pub get


3. Now we will use a package from pub.dev to make our task easy.

The package is called excel.

Install this package by adding dependency in pubspec.yaml and doing pub get.



4. Now let's create a function which accepts name of excel file as parameter and reads it. 

We use a map data structure to store data. But there can be two types of storing : row by row or column by column. Let's see both so that you can fit it according to your need.


a. row by row data storing

=> mp[i] - stores ith row elements

void readExcelFile(String fileName){

  ByteData data = await rootBundle.load("assets/$fileName");
  var bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  var excel = Excel.decodeBytes(bytes);
  int j=0;
  for (var table in excel.tables.keys) {
    Map<int, List<String>> mp = Map<int, List<String()>>();
    for (var row in excel.tables[table].rows)
        mp[++j] = row ; 
    }
}

=> For Animals.xlsx :

 mp[1]=[1,2,3,4,5];

mp[2]=['dog','owl', 'lion', 'eagle','panda']

 

b. column by column data storing

=> mp[i] - stores ith column elements


void readExcelFile(String fileName){

  ByteData data = await rootBundle.load("assets/$fileName");
  var bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  var excel = Excel.decodeBytes(bytes);

  for (var table in excel.tables.keys) {
    Map<int, List<String>> mp = Map<int, List<String()>>();
    int cols = excel.tables[table].maxCols;
    for (var row in excel.tables[table].rows) {
      for (int j = 1; j <= cols; j++) {
        mp[j].add(row[j - 1]);
      }
    }
  }
}

=> For Animals.xlsx :

 mp[1]=[1,'dog', 'cat', 'cow', 'hen'];

 Now you can use this data in any way you want maybe to store in database or directly use in app or some other way. I hope this article helped you and if you face any problem do comment below and I would be glad to help.

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!