Export Data to Excel and CSV file using PhpSpreadsheet library in CodeIgniter and MySQL

Export data functionality makes your web application user-friendly and helps the user to maintain list data.PhpSpreadsheet is a pure PHP library for reading and writing spreadsheet files. This tutorial will help you understand how Export Data to Excel and CSV files using PhpSpreadsheet library in CodeIgniter and MySQL.As PHPExcel is deprecated, so we will use PhpSpreadsheet to create an excel file with collection data.
Step 1: Download and install CodeIgniter.
Step 2: composer or direct download
Run below composer command to download phpspreadsheet library from your project folder. It will create a new folder called “vendor” and it will download phpoffice/phpspreadsheet library.
$ composer require phpoffice/phpspreadsheet Here is my the directory structure after installing phpoffice/phpspreadsheet
3.Setup Composer Autoload
You need to set vendor directory path inside application/config/config.php
Before started to implement the export data to excel and csv file using PhpSpreadsheet library in CodeIgniter and MySQL, look files structure:
  • codeigniter-export-excel-csv-file-data-into-mysql
    • application
      • config
        • autoload.php
        • constants.php
        • database.php
        • routes.php
      • controllers
        • Phpspreadsheet.php
      • models
        • Site.php
      • views
        • spreadsheet
          • export.php
        • templates
          • footer.php
          • header.php
    • vender
      • phpoffice
    • system
    • index.php
    • assets
      • css
      • upload
Step 4: Create MySQL Database and Table
The following SQL creates a customer table in the MySQL database.
Step 5: The Site model handles the database .
Create a file named Site.php inside “application/models folder.
Step 6: Create Controller and load class
Create a controller named Phpspreadsheet.php and use phpspreadsheet library inside controller.Please find below the code for controller.
Step 7: Create View:
Create a view named export.php inside application/views directory. Please find the code for view file.