Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to generate Excel using PhpSpreadsheet in CodeIgniter

In this post, I would like to show using of PhpSpreadsheet library within your CodeIgniter 3 project. PhpSpreadsheet is a pure PHP library for reading and writing spreadsheet files.

Similar Posts
CodeIgniter 4 – Export data to excel in CodeIgniter using PhpSpreadsheet
How to use PHPExcel with CodeIgniter?
How to generate excel from the array using PHPExcel

How to generate Excel using PhpSpreadsheet in CodeIgniter

Here are the steps to generate Excel in the CodeIgniter 3 application with PhpSpreadsheet:

Step 1: Download and install CodeIgniter.

Step 2: Run the below composer command to download phpspreadsheet the library from your project folder. It will create a new folder called “vendor” and it will download phpoffice/phpspreadsheet library into it.

$ composer require phpoffice/phpspreadsheet

Here is my the directory structure after installing phpoffice/phpspreadsheet

Step 3: Open application/config/config.php file and set your vendor directory path

 $config['composer_autoload'] = 'vendor/autoload.php';

Step 4: Use phpspreadsheet library inside in your controller, here is the sample:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Welcome extends CI_Controller {
    
	public function index()
	{       
		$spreadsheet = new Spreadsheet();
		$sheet = $spreadsheet->getActiveSheet();
		$sheet->setCellValue('A1', 'Hello World !');
		
		$writer = new Xlsx($spreadsheet);

		$filename = 'name-of-the-generated-file.xlsx';

		$writer->save($filename); // will create and save the file in the root of the project

	}

	public function download()
	{
		$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 

	}
}

For more information, here is the official documentation page of – phpspreadsheet documentation

  1. Hi Arjun,
    with your code I have problems when I try to open the file with Excel through the ‘Open with / Save’ menu.
    I had to substitute the line:
    header(‘Content-Type: application/vnd.ms-excel’);
    with:
    header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);

  2. I GET THIS ERROR IN THE FILE DOWNLOADED
    An uncaught Exception was encountered
    Type: Error

    Message: Class ‘PhpOfficePhpSpreadsheetSpreadsheet’ not found

    Filename: C:laragonwwwsitprogbdapplicationcontrollersWelcome.php
    Line Number: 11

    Backtrace:

    File: C:laragonwwwsitprogbd7d91e3-7a16-40e5-852c-a29107d38299index.php
    Line: 315
    Function: require_once

Leave a Reply