Getting Started for Syncfusion Flutter XlsIO

5 May 20214 minutes to read

This section explains the steps required to create a Excel document by few lines of code. This section covers only the minimal features needed to learn to get started with the Excel.

Steps to create Excel document in Flutter application

Create a simple project using the instructions given in the `Getting Started with your first Flutter app’ documentation.

Add dependency

Add the Syncfusion Flutter XlsIO dependency to your pub spec file.

  • dart
  • dependencies: 
    syncfusion_flutter_xlsio: ^xx.x.xx

    NOTE

    Here xx.x.xx denotes the current version of ‘Syncfusion Flutter XlsIO’ package.

    Get packages

    Run the following command to get the required packages.

  • dart
  • $ flutter pub get

    Import package

    Import the following package in your Dart code.

  • dart
  • import 'package:syncfusion_flutter_xlsio/xlsio.dart';

    Add a new button widget as a child of your container widget.

  • dart
  • @override
    Widget build(BuildContext context) {
      return Scaffold(
    	body: Center(
    	  child: RaisedButton(
    		onPressed: _createExcel,
    		  child: Text('Create Excel')
    		)
    	 )
      );
    }

    Include the following code snippet in the button click event to create a Excel file.

  • dart
  • Future<void> _createExcel() async {
    // Create a new Excel Document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Set the text value.
    sheet.getRangeByName('A1').setText('Hello World!');
    
    // Save and dispose the document.
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    // Save the Excel file in the local machine.
    File('Output.xlsx').writeAsBytes(bytes);
    
    }

    Create a Excel document in mobile

    You can create a Excel document in mobile by using the following steps:

    Add dependency

    Add the following packages to your pub spec file.

  • dart
  • path_provider: ^1.6.5
    open_file: ^3.0.1

    Import package

  • dart
  • import 'dart:io';
    import 'package:open_file/open_file.dart';
    import 'package:path_provider/path_provider.dart';

    Include the following code snippet to create a Excel document in mobile.

  • dart
  • Future<void> _createExcel() async {
    // Create a new Excel Document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Set the text value.
    sheet.getRangeByName('A1').setText('Hello World!');
    
    // Save and dispose the document.
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    // Get external storage directory
    final directory = await getExternalStorageDirectory();
    
    // Get directory path
    final path = directory.path;
    
    // Create an empty file to write Excel data
    File file = File('$path/Output.xlsx');
    
    // Write Excel data
    await file.writeAsBytes(bytes, flush: true);
    
    // Open the Excel document in mobile
    OpenFile.open('$path/Output.xlsx');
    
    }

    Create a Excel document in web

    You can create a Excel document in web by using the following steps.

    Import package

  • dart
  • import 'dart:convert';
    import 'dart:html';

    Include the following code snippet to create a excel document in web.

  • dart
  • Future<void> _createExcel() async {
    // Create a new Excel Document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Set the text value.
    sheet.getRangeByName('A1').setText('Hello World!');
    
    // Save and dispose the document.
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    //Download the output file in web.
    AnchorElement(
        href:
            "data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
      ..setAttribute("download", "output.xlsx")
      ..click();