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


        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(){
        public function index(){
        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/');
        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/', '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>";
  9. Creating View:
  10. inside application/views


        <title>Import/Export using phpspreadsheet in codeigniter</title>
        font-family: Verdana;
        font-size: 14pt;
        font-style: normal;
        font-weight: bold;
        text-align: center;
        font-family: Verdana;
        font-size: 12pt;
        font-style: normal;
        font-weight: bold;
        <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">
        <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>
        <?php echo form_close();?>
  11. Route Configuration:
  12. route configuration inside application/config/routes.php.

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