How to use phpspreadsheet in codeigniter?

    • Actions

      Automate any workflow

    • Packages

      Host and manage packages

    • Security

      Find and fix vulnerabilities

    • Codespaces

      Instant dev environments

    • Copilot

      Write better code with AI

    • Code review

      Manage code changes

    • Issues

      Plan and track work

    • Discussions

      Collaborate outside of code

    • Explore
    • All features
    • Documentation
    • GitHub Skills
    • Changelog

    • By Size
    • Enterprise
    • Teams
    • Compare all
    • By Solution
    • CI/CD & Automation
    • DevOps
    • DevSecOps
    • Case Studies
    • Customer Stories
    • Resources

    • GitHub Sponsors

      Fund open source developers

    • The ReadME Project

      GitHub community articles

    • Repositories
    • Topics
    • Trending
    • Collections

  • Pricing


  1. Download and install Codeigniter

    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.

  2. Installation of PhpSpreadsheet

    To download open command prompt and run command from your project root folder.

    $ composer require phpoffice/phpspreadsheet


  3. Setup Composer Autoload

    application/config/config.php.$config[‘composer_autoload’] = ‘vendor/autoload.php’;

  4. Creating Controller:

    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);
        }
        }
        }
        

  5. Creating View:

    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>
        

  6. Route Configuration:

    route configuration inside application/config/routes.php.

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


How to use phpspreadsheet in codeigniter?

How to use phpspreadsheet in codeigniter?