Open In App

Flutter – Create an Excel Sheet and Save the File in Device

Last Updated : 08 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A spreadsheet is a computer program that captures, displays, and manipulates data arranged in rows and columns. Sometimes we have data but that is either in json or in the list and we have to convert data to an excel sheet and store it in the user’s device. So that he/she can easily access it, you must have used the excel sheet earlier. So let’s make the make excel sheet in flutter and download it to our device.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Add this package syncfusion_flutter_xlsio to your project

Dart




dependencies:
  syncfusion_flutter_xlsio: ^20.4.50-beta


Step 3: To make an excel sheet we need some data in json form or a list

We will take this data to convert it into an excel sheet

[
    {
        "title": "How to Add .env File in Flutter?",
        "link": "https://www.geeksforgeeks.org/how-to-add-env-file-in-flutter/"
    },
    {
        "title": "Flutter - Select Single and Multiple Files From Device",
        "link": "https://www.geeksforgeeks.org/flutter-select-single-and-multiple-files-from-device/"
    },
    {
        "title": "Autofill Hints Suggestion List in Flutter",
        "link": "https://www.geeksforgeeks.org/autofill-hints-suggestion-list-in-flutter/"
    },
    {
        "title": "How to Integrate Razorpay Payment Gateway in Flutter?",
        "link": "https://www.geeksforgeeks.org/how-to-integrate-razorpay-payment-gateway-in-flutter/"
    },
    {
        "title": "How to Setup Multiple Flutter Versions on Mac?",
        "link": "https://www.geeksforgeeks.org/how-to-setup-multiple-flutter-versions-on-mac/"
    },
    {
        "title": "How to Change Package Name in Flutter?",
        "link": "https://www.geeksforgeeks.org/how-to-change-package-name-in-flutter/"
    },
    {
        "title": "Flutter - How to Change App and Launcher Title in Different Platforms",
        "link": "https://www.geeksforgeeks.org/flutter-how-to-change-app-and-launcher-title-in-different-platforms/"
    },
    {
        "title": "Custom Label Text in TextFormField in Flutter",
        "link": "https://www.geeksforgeeks.org/custom-label-text-in-textformfield-in-flutter/"
    }
   
]

Step 4: Create a button from where you can create excel and download it in

Dart




floatingActionButtonLocation:
          FloatingActionButtonLocation.miniCenterFloat,
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {},
        label: const Text("Create Excel sheet"),
      ),


Step 5: Import 

Dart




import 'package:syncfusion_flutter_xlsio/xlsio.dart' as xcel;


Step 6: Create an instance workbook and worksheet for 1st

Dart




final xcel.Workbook workbook = xcel.Workbook();
final xcel.Worksheet sheet = workbook.worksheets[0];


Step 7: Create a heading for the excel sheet in the first rows

Dart




sheet.getRangeByIndex(1, 1).setText("Title");
sheet.getRangeByIndex(1, 2).setText("Link");


Step 8: Now we will insert data into an excel sheet

Dart




for (var i = 0; i < myList.length; i++) {
     final item = myList[i];
     sheet.getRangeByIndex(i + 2, 1).setText(item["title"].toString());
     sheet.getRangeByIndex(i + 2, 2).setText(item["link"].toString());
}


Step 9: Save the excel sheet 

Dart




final List<int> bytes = workbook.saveAsStream();
FileStorage.writeCounter(bytes, "geeksforgeeks.xlsx", context);


Step 10: Dispose the workbook that you had created

Dart




workbook.dispose();


Step 11: Download the excel file downloads folder

Note: Add this permissions (For android)

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

Output:

1. Button to be clicked to create excel of data shown and save file

 

2. Snackbar after successfully saving the file in the device

 

3. File saved in the download

 

4. Saved File

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads