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

Using Excel and CSV file you can store all the data and import the Excel and CSV file data into the database at once using PhpSpreadsheet library in CodeIgniter and MySQL.PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Import Excel and CSV into MySQL help to save the user time and avoid repetitive work.
In this tutorial how to explain Import Data to Excel and CSV file using PhpSpreadsheet library in CodeIgniter and MySQL.As PHPExcel is deprecated, so we will use PhpSpreadsheet to create an excel file with collection data. We will cover this tutorial with a live demo to Import Data to Excel and CSV file using PhpSpreadsheet library in CodeIgniter and MySQL.
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 import data to excel and csv file using PhpSpreadsheet library in CodeIgniter and MySQL, look files structure:
  • codeigniter-import-excel-csv-file-data-into-mysql
    • application
      • config
        • autoload.php
        • constants.php
        • database.php
        • routes.php
      • controllers
        • Phpspreadsheet.php
      • models
        • Site.php
      • views
        • spreadsheet
          • index.php
          • display.php
        • templates
          • header.php
          • footer.php
    • vender
      • phpoffice
    • system
    • index.php
    • assets
      • css
      • uploads
        • sample-xlsx.xlsx
        • sample-xls.xls
        • sample-csv.csv
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 index.php inside application/views directory. Please find the code for view file.
Step 8: Create View:
Create a view named dispaly.php inside application/views directory. Please find the code for view file.