A simple Class to export data to excel using PHP
- Sunday, December 16, 2007, 18:21
- php
- 26 comments
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% [?]
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





Would this class export using .xls and .csv ?
Php Programmers
sorry for the confustion..this class export data in .xls format.
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.
nice thanks!
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.
does your class can export image from php to excel?
Thanks, It works like charm. simple class using multi dimention(3) array.
Thanks again for time saving
nice to hear that…..
True XLS needs to escape data and may contain multiple sheets.
Nice Post. Thanks for Sharing.
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!
Thanks guru for keeping those copyright informations……..
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?
Thanks it works beautifully.But I cant able to perform export into excel using Ajax in php by using same script . Please help me.
very nice script, I’ve been looking for something like this for a while. :- )
Do you have any simple Class to Import data of excel file to mysql using PHP
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.
It’s awesome code.., really i liked it. Thanks for sharing this information with us.
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
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.
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
I need, an php code to export data from php page to Excell page, please give me full example code
Thanks code. It’s good. thks so much
This is awesome . Have you any code to export into pdf .
Thanks Victor…Good to hear from you
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