CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New
Need a Website Or Web Application Contact : +91 9437911966 (Whatsapp) Note: Paid Service

How to Import and Export using PhpSpreadsheet Library in Codeigniter


  1. Download and install Codeigniter
  2. We already discuss know about the setup of Codeigniter. If you have any issue with the Codeigniter setup then you can check this post Codeigniter Setup.

  3. Installation of PhpSpreadsheet
  4. To download open command prompt and run command from your project root folder.

    $ composer require phpoffice/phpspreadsheet


  5. Setup Composer Autoload
  6. application/config/config.php.$config[‘composer_autoload’] = ‘vendor/autoload.php’;

  7. Creating Controller:
  8. PhpspreadsheetController.php and use phpspreadsheet library inside controller

    PhpspreadsheetController.php

        <?php
        defined('BASEPATH') OR exit('No direct script access allowed');
        use PhpOffice\PhpSpreadsheet\Spreadsheet;
        use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
        class PhpspreadsheetController extends CI_Controller {
        public function __construct(){
        parent::__construct();
        }
        public function index(){
        $this->load->view('spreadsheet');
        }
        public function export(){
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $sheet->setCellValue('A1', 'Hello World !');
        $writer = new Xlsx($spreadsheet);
        $filename = 'name-of-the-generated-file';
    
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'. $filename .'.xlsx"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output'); /* download file */
        }
        public function import(){
        $file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        if(isset($_FILES['upload_file']['name']) && in_array($_FILES['upload_file']['type'], $file_mimes)) {
        $arr_file = explode('.', $_FILES['upload_file']['name']);
        $extension = end($arr_file);
        if('csv' == $extension){
        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
        } else {
        $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        }
        $spreadsheet = $reader->load($_FILES['upload_file']['tmp_name']);
        $sheetData = $spreadsheet->getActiveSheet()->toArray();
        echo "<pre>";
        print_r($sheetData);
        }
        }
        }
        
  9. Creating View:
  10. inside application/views

    spreadsheet.php

        <html>
        <head>
        <title>Import/Export using phpspreadsheet in codeigniter</title>
        </head>
        <body>
        <style>
        h3
        {
        font-family: Verdana;
        font-size: 14pt;
        font-style: normal;
        font-weight: bold;
        color:red;
        text-align: center;
        }
    
        table.tr{
        font-family: Verdana;
        color:black;
        font-size: 12pt;
        font-style: normal;
        font-weight: bold;
        text-align:left;
        }
    
        </style>
        <h3><u>Import/Export using phpspreadsheet in codeigniter</u></h3>
    
        <?php echo form_open_multipart('spreadsheet/import',array('name' => 'spreadsheet')); ?>
        <table align="center" cellpadding = "5">
        <tr>
        <td>File :</td>
        <td><input type="file" size="40px" name="upload_file" /></td>
        <td class="error"><?php echo form_error('name'); ?></td>
        <td colspan="5" align="center">
        <input type="submit" value="Import Users"/></td>
        </tr>
        </table>
        <?php echo form_close();?>
        </body>
        </html>
        
  11. Route Configuration:
  12. route configuration inside application/config/routes.php.

        $route['spreadsheet'] = 'PhpspreadsheetController';
        $route['spreadsheet/import'] = 'PhpspreadsheetController/import';
        $route['spreadsheet/export'] = 'PhpspreadsheetController/export';