Working with Spreadsheets in Flutter: Part 1
Tags: Spreadsheet, Flutter, Excel, Dart, Xlsx

Deep dive into spreadsheets in flutter

Introduction

Hello friends today we will be learning how to create, load, update and save spread sheets in flutter.

This is an introduction and the first article in a series of articles where we will be exploring Spreadsheets in flutter.

Caution

This tutorial focuses on working with “.XLSX” only, Tutorial of how to work with .CSV files will be available soon

Let’s Go!!!

The first thing we need to do is to add excel package to your pubspecp.yaml file. Click on the “excel” above to get the latest version of excel package. I’m currently using:

excel: ^1.1.5

which is the latest as at the time I am writing this article. Then run flutter pub get to get the package.

Let’s code

First of all you need to add import for excel

import ‘package:excel/excel.dart’;

I assume you already have a project you want to use so we won’t talk about that

Create an excel File

var excel = Excel.createExcel();

This line of code create a excel file for you with one sheet called “ Sheet1 ”

There you go!! you have created your first excel file.

Inserting data into an excel file

Now that we have an excel file, we can move on to inserting files into the file. there are two ways of inserting data into an an excel file:

Getting reference to a single cell in the a sheet

Excel file with multiple sheets

Sheet sheetObject = excel['Sheet1']; var cell = sheetObject.cell(CellIndex.indexByString("A1"));

cell.value = 8; // dynamic values support provided;

lets diffuse this lines :

Sheet: A sheet is a single page inside of an excel file, if you are familiar with excel you will understand that a single excel file can contain multiple sheets

excel[‘sheet1’]: Each sheet in an excel file has a name, so to get reference to the sheet you need it’s name

var cell = sheetObject.cell(CellIndex.indexByString("A1"));

This line of code is used to get reference to cell “A1”, which as we know is the first cell in the sheet

cell.value = 8;

This line sets the value of cell “A1” to 8

2. Inserting values into a row with List<String>

List<String> data = ["Mr","Joseph", "Isiyemi"];

Sheet sheetObject = selectedExcel["Sheet1"]; sheetObject.appendRow(data);

lets diffuse this lines :

we already know what sheets and sheetObjects are.

sheetObject.appendRow(date): This Line of code inserts the List values into the sheet like this:

Inserting a row into an excel file

I want to see my Excel Document in my phone !!!

For us to be able to view our Excel document we have to first add

permission_handler: ^5.0.1+1

to our pubspec.yaml file then save the file to our download folder like this:

First things first to get access to download folder in android you have to add the following to your Manifest file :

android\app\src\main\AndroidManifest.xml



<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application.....

if you are testing on and android 9 and above device you should add the following to you application tag inside your Manifest file:

<application android:requestLegacyExternalStorage="true" // add this line android:label="upload" android:icon="@mipmap/ic_launcher">

You’re good to go after this.

Our final Excel file will look like this

Final Excel File

We’ve come to the end of the first of this series, next we will be looing into loading , Deleting and updating an existing Excel file. Let me know what you think in the comment section and don’t forget to give a clap if you learnt a thing or two.