A simple Class to export data to excel using PHP
Posted on December 16, 2007
Filed Under 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.Popularity: 39% [?]
If you like this post then please subscribe to my full RSS feed . You can also subscribe by email and have new posts sent directly to your inbox.And, You can also follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» A ajax tutorial for beginners
» Parsing the XML in easy way using PHP
» Uploading large(big) files in PHP using .htaccess
» How to redirect browser to https (ssl) in php
Comments
10 Responses to “A simple Class to export data to excel using PHP”
Leave a Reply





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.