How to use PHPExcel with CodeIgniter?

PHPExcel is a pure PHP library for reading and writing spreadsheet files and CodeIgniter is one of the well known PHP MVC Framework. In this tutorial, I am gonna show you how to Integrate PHPEXcel library in CodeIgniter with simple steps.

Setp1 : Download and setup CodeIgniter.(download it here:
Setp2 : Download PHPExcel.(download it here:
Setp3 : Unzip or extract the downloaded PHPExcel lib files and copy Class directory inside files to application/third-party directory(folder).
setp3: Now create one file called EXcel.php in application/library folder [application/library/Excel.php]. Then include PHPExcel Class to it. see how i did it below

That’s it now you can use PHPEXcel methods inside your CodeIgniter application.But you should load it before using it methods(Ex: $this->load->library(‘excel’);)

How to read excel file

How to Create excel file on the file

How to Generate Excel file with Database table data

That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.


I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.

  • Nilesh Daldra

    This example gives garbage characters in excel file…by google i found issue in this code is…
    it should

    instead of

    it i also need some clean code..

  • Mhmoud

    if you please can you add an image to header page in your example?

    • Header page means? Could you please explain this in detail


        It means He/She wants to add a Image in excel sheet on top cells like header.

  • kabeza

    Thanks for the little tutorial and snippet. Worked great

  • Bongle Pappu

    Ever heard of plagiarism? How do you implement it in PHP? Can you cook curry with plagiarism?

  • Nick Naja

    Awesome Dude!!


    That greate Dude!!

  • Mufaddal Saifee

    Thanks Arjun, Great Tutorial Really helpful… 🙂

  • Sandy

    Hi dude , i have a problem when i accessed the page to test php excel it return webpage not available

    could you describe it to me ?

  • Baman SB

    Clean and simple. TNX Arjun.

  • Trevor Albrecht

    Can you have a pre-formatted spreadsheet and use this to populate it with data?

  • raknjak

    Ah a neit little trick that I can use for everything, great!

  • aya fers ∞

    i’m having an error:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: CI_Loader::$excel

    Filename: pages/excel.php

    Line Number: 5


  • Thanks. It works perfectly for me

  • mahesh

    Hey its not working for me. But when I run your examples, they works perfectecly. I just copied your export code still getting garbage data when I open downloaded xls file. Thanks

  • Abhijith S

    Cool, Its working perfectly. Thank you.

  • Dileep Kumar

    export excel is not working please help me out.

  • Deepak Prasad

    How to config google map api with multiple location dynamically in codeigniter

  • chabilita

    do you know what serialize and unserialized is ? can you please give me some example. thank you 🙂

    • Serialize() converts an array into a normal string that you can pass in a URL,save as in database(wordpress uses) etc.
      Unserialize() is the opposite of serialize() – it takes a serialized string and converts it back to an array.

      • chabilita

        Can you please give example sir . thank you 🙂

        • In couple days ,will write a post

          • chabilita

            I’ll Wait, Thank you 🙂

  • amandat

    I had to add


    before the last line for it to work. Otherwise, great, quick how-to. Thanks.

    • Marco Antonio Aguilar Gomez

      You saved my life. Thx!

  • leeyoo

    Hello, Whenever I call this controller I get the following error:A PHP Error was encountered

    Severity: Error

    Message: Class ‘PHPExcel_IOFactory’ not found

    Filename: controllers/Result.php

    Line Number: 229
    line229: $objReader = PHPExcel_IOFactory::createReader(‘Excel2007’); please help me

    • Pathy

      you are configured not correct pls follow as arjun declared

  • Muhammad Uzair Khan

    Nice trick. (Y)

  • Pathy

    good job ajjun ..its work on create excel only..i cant read excel how
    thanks in advance ..

    • are you able to open your base excel sheet.

      • Pathy

        yes i can open base excel sheet .
        arjun..i need to retrieve data from mysql table to excel with all rows and colums as seen in tables..i can receive single row of data only …column name as we proceeded in phpexcel code.
        i need multiple rows with column names to assiciate excel sheet ..
        how it is.
        and i have to split rows as number sets (2000/20)…and once again i will to upload to mysql ..

        this may have any chance pls help me do it..

        • Hi , Have updated the post. It will lead you to achieve your requirement.

          • Pathy

            thanks arjun
            we need table colums in first row in excel
            we have 5000 questions and options and answers in the same table
            we have to spilit them all in sets cinsists of 500 per set and inner set should have 20 each..
            how to split them in excel ..
            can have a trick pls help me do

          • you have to push table columns name into result array

          • Pathy

            yes thank for your reply arjun ..try to understand the picture .i m waitning for your response

          • Pathy

            now im trying to push column in to result array
            thank you again arjun

          • my question is how you want to display the result, any sample excel

          • Pathy

            table having 13 columns and multiple rows per test ok ..
            we need to save 20 per excel sheet up to 500 *20 per test if test question having 5000 qn. for example ..
            i cannot create xls column with rows perfectly ..i m used array_merge its wrking on print_r() function only ..but its too print rows and columns as consequent index ..
            rows are printed as rows and columns are printed as rows when using array_merge function ..
            any possiblities pls i m in hurry ..

          • Pathy

            how to send files to you

          • can you share visual representation of your requirement

        • you just need to call fromArray() method on active sheet.


  • Saket Sharma

    thanku bro it helped a lot

  • Bakhtawar Mubeen

    arjun I am doing in HMVC and confused where to save my excel file and where I can save my php file.

    • Let me know which HMVC module are you using, so that I can give proper solution

      • Bakhtawar Mubeen

        I am doing in seller module.and don’t know where to put the excel file and wher to put

  • reedo88

    Hi Arjun, this is great, thankyou. But do you know what mime type to use for Excel2007?

    • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

      • amandat

        I’ve tried every configuration I can find for the Excel2007 and not having any luck. Have you? I need a chart and charts don’t work in the Excel5…

  • amandat

    This works beautifully for Excel5 but when I try to update for Excel2007, I get nothing or a file that won’t open. I have tried every configuration of this (with and without ob_end_clean()). Is CodeIgniter doing something to get in the way?

    Excel5 works great:
    header(‘Content-type: application/’);
    header(‘Content-Disposition: attachment;filename=”testfile.xls”‘);
    header(‘Cache-Control: max-age=0’);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5’);

    Excel2007 not so much:
    header(‘Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);
    header(‘Content-Disposition: attachment;filename=”testfile.xlsx”‘);
    header(‘Cache-Control: max-age=0’);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007’);

  • Radha

    I try to generate excel with database,but it show error as Non-existent class: CI_Excel,where i did mistake?

  • Kewal Kanojia

    Hello Arjun,

    I using this but facing problem while export data from database into excel. Please see attach file.

    • Pankaj More

      used ob_end_clean(); before header method


        put this line under success action if you are using ajax:
        like this.
        window.location = site_url + ‘your controller/you function’;


      you can put this line under success action if you are using ajax:
      like this.
      window.location = site_url + ‘your controller/you function’;

  • Sathish Siva

    hai arjun can u tell me which portion cover under model and view and controller . i am new for framework

  • Sathish Siva

    how to get the database table data using laravel4, can u send source code here which parts belongs to model and view and controller

  • Ahmad

    Hello Arjun, how can i add heading in excel?

  • igor

    Arjun, I was using your code with writing to Excel5 but the result file is like the one that Kewal Kanojia’s has. Any hints to solve it?


      put this line under success action if you are using ajax:
      like this.
      window.location = site_url + ‘your controller/you function’;

  • igor

    By the way, “How to Create excel file on the file” should probably be “How to Create excel file on the fly”, shouldn’t it?

  • igor

    Arjun, you’ve posted: “we are here to solve your problems.” Why then you do not reply requests for help? If you do not know how to solve it – write it in comment.

  • irene baluis

    do you have a code for looping data in the cells ?

  • Shrikant Gupta

    Hi, Arjun
    thanks for this amazing tutorial, i follow all steps and its work fine on my local server but when i was upload this code live server i am getting (file not found) page. then i used ob_end_clean and some other code like change headers but nothing is going well, when i comment the last line of code $objWriter->save(‘php://output’); now its work fine and but blank excel-file download can you help me please. thanks in advance.


  • Sai

    Thank, but if export more than 100 row it not work
    And No error Just print the row
    Can u explain me why it like that

  • Ajit Gurung

    Hi Arjun,
    How can I export the array in a column instead of a row?
    and How can I add the column header?

  • Vandolph Reyes

    Thanks mate, works great.

  • dhaval



    SpreadsheetUnknown CreatorUnknown
    [email protected]€×¢wR€Ò@€×¢wR€Ò

    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    àõÿ À
    à À
    is just some text value








    de calculRoot Entryÿÿÿÿÿÿÿÿ

    • dhaval

      when i generate excel. its display in excel.php

  • Abdurrahman Ukasyah

    thx, i was helped.

  • José Antonio Vivas Acosta

    Muchisimas gracias desde Venezuela, me sirvio mucho tu aporte… Thank you very much!

  • saroj sahu

    Very help full post I have a query what if we want to download the same excel file in our directory , I am not asking about browser download option.


  • ikramullah mohmand

    Salam Arjun,
    This was damn easy and helpful, sharing is caring, Saved my day.
    Love you.


  • Ashiq Muhammed

    when i created excel sheet xlsx file , that can be currepted , what i can do for that error ? Protection Status