A simple Class to export data to excel using PHP

Advertisement

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.

Popularity: 18% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Creating and Parsing JSON data with PHP
» How to filter user submitted data easily in PHP?
» How did I reduce CPU overhead problem caused by MySql?
» Uploading large(big) files in PHP using .htaccess

26 Comments on “A simple Class to export data to excel using PHP”

  • Chrisranjana wrote on 17 December, 2007, 9:51

    Would this class export using .xls and .csv ?

    Php Programmers

  • Roshan wrote on 17 December, 2007, 15:50

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

  • will wrote on 19 January, 2008, 0:39

    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.

  • tom wrote on 19 January, 2008, 3:46

    nice thanks!

  • Billy wrote on 27 February, 2008, 15:49

    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!
    Thanks.

  • acidsneaker wrote on 1 April, 2008, 3:10

    does your class can export image from php to excel?

  • Learn Hacking wrote on 8 May, 2008, 16:42

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

    Thanks again for time saving :)

  • Roshan wrote on 9 May, 2008, 10:40

    nice to hear that…..

  • php-web-developer wrote on 20 May, 2008, 7:24

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

  • Web Hosting Sri Lanka wrote on 22 May, 2008, 6:54

    Nice Post. Thanks for Sharing.

  • Guru wrote on 26 March, 2009, 7:54

    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! :)

  • Roshan wrote on 26 March, 2009, 17:18

    Thanks guru for keeping those copyright informations……..

  • MilanG wrote on 7 June, 2009, 9:59

    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?

  • Daulat Rajbanshi wrote on 20 July, 2009, 12:07

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

  • aestrada wrote on 3 August, 2009, 19:20

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

  • Amit wrote on 9 October, 2009, 6:40

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

  • Clem wrote on 8 December, 2009, 19:10

    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.

  • Vijaya Kumar S wrote on 1 January, 2010, 11:28

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

  • marina wrote on 15 January, 2010, 9:48

    Hi!

    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?

    Thanks

  • marina wrote on 15 January, 2010, 10:36

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

    Bye.

  • Victor Gurung wrote on 24 February, 2010, 8:13

    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

  • Mohammed wrote on 26 February, 2010, 20:07

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

  • quocviet wrote on 8 March, 2010, 5:55

    Thanks code. It’s good. thks so much

  • Sheraz Qamar wrote on 8 March, 2010, 6:33

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

  • Roshan wrote on 16 March, 2010, 4:40

    Thanks Victor…Good to hear from you

  • 2lajazz wrote on 4 May, 2010, 16:23

    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.
    Thankx

Write a Comment

 


Copyright © 2010 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.