PHP将MySql表导出到Xls
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP将MySql表导出到Xls相关的知识,希望对你有一定的参考价值。
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!
<?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(); ?>
以上是关于PHP将MySql表导出到Xls的主要内容,如果未能解决你的问题,请参考以下文章