A simple Class to export data to excel using PHP

While generating a report in your project, you might have to download the data into excel file using PHP. In most scenario, you have to display the report in a page and create a link to download the report in the excel file. Well in that scenario, i think i can help you with a simplified class and code of PHP.

I’ve created three files in my example.

1) class.export_excel.php – This file contains the code to export data in excel.

2) export_excel.php – This file contains the code to assign values of the excel header and excel data. The header is one-dimentional array. And the other one is the values to be exported and this one is two-dimentional array. And this file also contain the “fn” named get method ($_GET) varible , which is the name of the file to be generated. If a file called “daily_report.xls” is to be generated then this file can be called as “export_excel.php?fn=daily_report”.

3) test.php – This file contains the data to be displayed in the web page and the same one will be exported in the the excel file. The heading and the values are stored in the session variables. The $_SESSION[‘report_header’] variable contain the headers of the excel file and $_SESSION[‘report_values’] contains the data of the excel.

Remember that $_SESSION[‘report_values’] is a two dimentional array and the column of the array should be same as the no of headers stored in the $_SESSION[‘report_header’] variable.

Now thats all, you can have a link to export_excel.php file with the the name of file to be generated as fn variable i.e. the call to the export_excel.php should export_excel.php?fn=report for generating the file name “report.xls”.

You can download the full source code by clicking here.

If you have any problem or comment, please contact me at bhattarairoshan[at]yahoo[dot]com.

The computer emergency readiness team, cert formulate such strategies to check the invasive and malignant trends of cyber networks. The Microsoft Certified Desktop Support Technician, mcdst exams are very significant for the computer technicians and engineers. If IT professionals want to improve the managerial skills emphatically, itil training is most suitable for them. It is not prerequisite to get full knowledge about the networking administration but the candidates of network plus are recommended to do this job efficiently for better success. If IT professional most advanced career, mcsa certification is very instrumental to achieve the target. The Microsoft certified system engineers, mcse certification is very important for the system analysts and IT engineers to get more expertise in certified manner. The security training is best way to guide the security professionals about all technical strategies.

33 thoughts on “A simple Class to export data to excel using PHP

  1. Chrisranjana

    Would this class export using .xls and .csv ?

    Php Programmers

  2. Roshan

    sorry for the confustion..this class export data in .xls format.

  3. will

    Your class only supports CSV.
    True XLS needs to escape data and may contain multiple sheets.

    For a better class to export to .XLS go to http://www.phpclasses.org and download it from there.

  4. tom

    nice thanks!

  5. Billy

    HI. I found your script very helpful. It is small and easy. I want to add it on my site but I have to ask something… Can you modify the code to align the text like:fonts,alignment,size etc?

    Good job!

  6. does your class can export image from php to excel?

  7. Thanks, It works like charm. simple class using multi dimention(3) array.

    Thanks again for time saving :)

  8. nice to hear that…..

  9. True XLS needs to escape data and may contain multiple sheets.

  10. Nice Post. Thanks for Sharing.

  11. This is awesome code. I have used it and am 100% satisfied with it. Very simple, easy to understand, and most importantly – it works perfectly. Thanks for sharing it Roshan. I am using it for my other website and guess what, I have kept your name & other copyright info as it is! :)

  12. Thanks guru for keeping those copyright informations……..

  13. Nice, it’s working well, but what’s with utf8 support?
    I tried to add just header parameter:

    header(“Content-type: application/vnd.ms-excel; charset: utf-8″);

    , but this doesn’t work. What else I have to do to make UTF8 charset working?

  14. Daulat Rajbanshi

    Thanks it works beautifully.But I cant able to perform export into excel using Ajax in php by using same script . Please help me.

  15. aestrada

    very nice script, I’ve been looking for something like this for a while. :- )

  16. Do you have any simple Class to Import data of excel file to mysql using PHP

  17. Clem

    I’m having a problem with the script that I cannot figure out. I am grabbing data from a database in three sets, sending the data from each set to an Excel file before pulling the next set. I am giving each Excel file a different name, as well. The problem I am having is that the script only creates one Excel file AND appends the data from each set horizontally next to the data from the preceding data set. Any suggestions on how to fix this are appreciated.

  18. It’s awesome code.., really i liked it. Thanks for sharing this information with us.

  19. marina


    I’m trying to export a date from my databasa to excel, using an array but in some computers te excel changes the date.

    Well the thing is that the dates like 04/05/2009 (4th of May) in the excel it’s like 05/04/2009 (5th of May), and the dates that the excel could not invert are showed like text.

    ┬┐is there any way to solve it with the class?


  20. marina

    Hi everybody!

    Answering myself, and for anybody who could have the same problem the solution I have used is to put a header in the class file that tells the file that is a file in spanish (in my case).

    Adding this line at end of the file where the headers are declared.

    header(“Content-Language: es”);


  21. Hi Roshan
    It was really gud to see ur site and especially the nepali people in techy world. Myself m a drupal and joomla developer working back in india. Your site has gud input. Keep it up

  22. Mohammed

    I need, an php code to export data from php page to Excell page, please give me full example code

  23. Thanks code. It’s good. thks so much

  24. Sheraz Qamar

    This is awesome . Have you any code to export into pdf .

  25. Thanks Victor…Good to hear from you

  26. 2lajazz

    Hi Roshan,
    Very great work with this script. I have a php table of three-dimentional array. How can i export value with your class.

  27. Awesome, thanks alot!

  28. Good A+++ ,
    i like this code to use easy export…

  29. Sumeet Gupta

    I can export the data to an excel file, using a few headers. however, the machines in my org are moving towards Office 2010, which means using excel 2010 for opening the xls files. Now, when the excel 2010 tries to open this file, it gives an error message saying “the format of the file is incorrect. do you want to open the file….”. When i click on yes, the file opens correctly. But how do i make sure that the file that i am generating is also compatible with Excel 2010? The followin are the headers which i use:-

    header(“Content-type: application/vnd.ms-excel”);
    header(“Content-Disposition: attachment; filename=$filename.xls”);
    header(“Cache-Control: private”);
    header(“Pragma: cache”);
    header(“Expires: 0″);

  30. MHD

    Thank you … that is very great class …

  31. mystarrocks

    People, can anyone help me? Actually, what will my Javascript function contain? Just the call to the PHP?
    The scenario is that I have a table of data and a Export to excel on clicking which, an excel file of the table data needs to be generated. Please help me know how I could get through this..

  32. shivam

    thanks..its absolutely amazing.

  33. Rahmad

    Thanks, it’s very helpfull my project. Keep good posting

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>