/ Published in: PHP
Face it!
Whenever you make a website/app you always need to make XLS export for the simple people. I mean heaven to betsy if they have to take a csv file, or use phpMyAdmin.
So
Here are 3 little functions and a sample skeleton that I use from time to time when I need to export out some data to an xls file.
Place the file in a safe place or if you want a completely open to the world place... :D Then replace the XXXXX's with the appropriate data.
Give the pointy haired boss the links!
Expand |
Embed | Plain Text
<?php // DB TABLE Exporter // // How to use: // // Place this file in a safe place, edit the info just below here // browse to the file, enjoy! // CHANGE THIS STUFF FOR WHAT YOU NEED TO DO $dbhost = "127.0.0.1:3306"; $dbuser = "XXXXX"; $dbpass = "XXXXX"; $dbname = "XXXXX"; $dbtable = "XXXXX"; // END CHANGING STUFF // first thing that we are going to do is make some functions for writing out // and excel file. These functions do some hex writing and to be honest I got // them from some where else but hey it works so I am not going to question it // just reuse // This one makes the beginning of the xls file function xlsBOF() { return; } // This one makes the end of the xls file function xlsEOF() { return; } // this will write text in the cell you specify function xlsWriteLabel($Row, $Col, $Value ) { echo $Value; return; } // make the connection an DB query $q = "SELECT * FROM ".$dbtable.""; // Ok now we are going to send some headers so that this // thing that we are going make comes out of browser // as an xls file. // //this line is important its makes the file name // start the file xlsBOF(); // these will be used for keeping things in order. $col = 0; $row = 0; // This tells us that we are on the first row $first = true; { // Ok we are on the first row // lets make some headers of sorts if( $first ) { foreach( $qrow as $k => $v ) { // take the key and make label // make it uppper case and replace _ with ' ' $col++; } // prepare for the first real data row $col = 0; $row++; $first = false; } // go through the data foreach( $qrow as $k => $v ) { // write it out xlsWriteLabel( $row, $col, $v ); $col++; } // reset col and goto next row $col = 0; $row++; } xlsEOF(); ?>
Comments
Subscribe to comments
You need to login to post a comment.

Thank u for the code....But how can we increase the column width of the xls file
You don't the Excel has a default size. The best thing to do is select all Ctrl-A then double click the edge of the first column and all columns will spread out to the size that is needed. This code here makes the absolute most basic excel file possible. There might be some other php libs out to do more fancy stuff.
Hi, just tried to implement this - I am getting a load of "Warning: Cannot modify header information - headers already sent by (output started at /path/testdl.php:6) in /home/sites/atkinsintelligence.com/public_html/RBS/testdl.php on line 77" through to line 87. When you open up the page testdl.php its not opening as an excel file just test in the browser.
Hi, just tried to implement this - I am getting a load of "Warning: Cannot modify header information - headers already sent by (output started at /path/testdl.php:6) in /home/sites/atkinsintelligence.com/public_html/RBS/testdl.php on line 77" through to line 87. When you open up the page testdl.php its not opening as an excel file just test in the browser.
hmmmm
What is happening according to your error is that on line 6 of your php you are outputting something... in my file line 6 is a comment...
So when you get down to the part where the php is constructing and xls file and not an text/html file the web server is saying:
"Yo jerk! your already started outputting text/html we can start output binary/xls"
Check you line 6, post your code?
Thanks a lot for this code....it works fine :) I messed around for a while with more 'complex' codes, with no acceptable result.
good work!
I use this base script numerous times. Normally I add a quick "if" there to convert "date" and "timestamp" fields from integers to human readable dates. I also have used this in combination with another html form to make a universal db exporter.
This here is simply the base.
Thanks, Sorry this is probably a simple error, but it is not writing to a file, it is writing to my webpage in a manner as follows ����� �1210049297 This is something that would be a great asset to me if I could get it to work
Sorry about the multiple posts, I refreshed the page and each time I just noticed it was posting the same details again
thats odd.
check to make absolutely sure that you are not already starting output some where else in your program. A blank line of html will tell apache that this is now text and no longer allow the binary header stuff to go.
You output looks like binary stuff, so really I believe that you are already outputting to the screen somewhere and you have error reporting turned off. Otherwise you would see an error like above: Warning: Cannot modify header information - headers already sent by
How can I save the file to the server and not the users computer?
probably by outputing the data to file instead of out to the screen? So instead of "echo" add the data to a temp string, then write the big temp string to a file. fileputcontents( $temp , $somefile ).
Hi, been using this code for a while, but have now noticed that it is limited to 255 characters in each cell. Any db table field that holds any more than 255 characters it just doesnt export that field into a cell in Excel.....is there a way to increase the limit?
http://stackoverflow.com/questions/7044790/problem-downloading-data-to-excel-using-php-pack-function
talks about the char max. Never needed to have more than 255 chars in a cell. Take a look.