First solemnly declare. This article is my original. Help the home reprint my article can indicate the source of the article reproduced? http://blog.csdn.net/luosisan/article/details/41749701 This is the younger brother's last article, by this help the home of the House of the audacity of copy and paste. And even the picture watermark is not modified, you can go to see my article Class table. This is to help the home of the link http://www.bkjia.com/sjkqy/922621.html, reprint no problem. Absolutely OK, but you have to say it, so at least it means that I have to fight so many words of respect. All right, don't say it. It's a little far. I hope it won't be a precedent.
In the work may encounter PHP export Excel situation, there are many ways online, there is a very simple method, I do not mention. If you are interested, you can search. But this method is not really generating Excel tables. To actually generate an Excel table, you need to use the Phpexcel class. At the end of the article I will give the download address. How do I use it after downloading? Here is a detailed source code example:
date_default_timezone_set ("etc/gmt-8").//Set time zone
Header (' content-type:text/html; Charset=utf-8 '); Set the page encoding method, preferably Utf-8
Require_once './classes/phpexcel.php '; Paths are set according to the path of your actual project
$objPHPExcel = new Phpexcel (); Creating an Phpexcel instance
Here is the connection to the MySQL database
$conn = mysql_connect (" localhost"," root ","123456") or Die (" Database connection failed! ");
mysql_select_db ("Test", $conn); Connecting to a database
mysql_query ("Set names ' GBK '"); Convert character encoding
$sql = mysql_query ("SELECT * from Goods_info"); Querying SQL statements
/*--------------Set HEADER information------------------*/
$objPHPExcel->setactivesheetindex (0)
->setcellvalue (' A1 ', ' ID number ')
->setcellvalue (' B1 ', ' Product name ')
->setcellvalue (' C1 ', ' article number ')
->setcellvalue (' D1 ', ' commodity barcode ')
->setcellvalue (' E1 ', ' Model specification ')
->setcellvalue (' F1 ', ' hanging quotes ');
/*--------------start inserting information from the database into the Excel table------------------*/
$i = 2; Defines an I variable that is designed to control the number of rows in the loop output data
$rows =array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ');
while ($rs =mysql_fetch_array ($sql)) {
$RM = Iconv ("GBK", "Utf-8//ignore", $rs [1]); Encode characters to convert GB2312 in the database to UTF-8 format
$objPHPExcel->setactivesheetindex (0)
->setcellvalue ("A". $i, $rs [0])//fill in the data in the cell
->setcellvalue ("B". $i, $RM)//Because my column is Chinese, I coded it on it.
->setcellvalue ("C". $i, $rs [2])
->setcellvalue ("D". $i, $rs [3])
->setcellvalue ("E". $i, $rs [4])
->setcellvalue ("F". $i, $rs [5]);
$i + +;
}
/*--------------The following settings for additional information------------------*/
$objPHPExcel->getactivesheet ()->settitle (' Example1 '); Set the name of the sheet
$objPHPExcel->setactivesheetindex (0); Set the starting position of the sheet
Note that the following is excel2007. Do not write excel5 or it will be garbled.
If you're still garbled, look at how your own data is encoded.
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
Above is the phpexcel_iofactory of the above data through the Write function
$outputFileName = "test. xls";
Header your browser and tell it to force the download instead of running the file in the browser
Header ("Content-type:application/force-download");
Header ("Content-type:application/octet-stream");//File stream
Header ("Content-type:application/download"); Download file
Header (' content-disposition:attachment;filename= ' $outputFileName ');
Header ("Content-transfer-encoding:binary");
Header ("Expires:mon, Jul 1997 05:00:00 GMT");
Header ("last-modified:".) Gmdate ("D, D M Y h:i:s"). "GMT");//Last Modified time
Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");
Header ("Pragma:no-cache"); Do not cache pages
$objWriter->save (' php://output '); Output to Browser
?>
This link is my source code, http://download.csdn.net/detail/luosisan/8255551. These include the Phpexcel Class I said above. Little brother is also just getting started with PHP. What's wrong with the hope that the big God is not strange
The above describes the PHP export excel2007 table, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.