php export excel2007 table

WBOY
Release: 2016-08-08 09:32:35
Original
1281 people have browsed it

Let me make a solemn statement first. This article is original to me. Can Bangkejia reprint my article to indicate the source of the article? http://blog.csdn.net/luosisan/article/details/41749701 This is my last article, which was copied and pasted by this helper without shame. And even the image watermarks are not modified. If you don’t believe it, you can check out my article category table. This is the link to Bangke Home http://www.bkjia.com/sjkqy/922621.html. There is no problem in reprinting. It's totally ok, but you have to say it, so that you can at least show my respect for my hard work in typing so many words. Okay, no more. That's a bit far. I hope this will not be an example next time and let’s get to the point

You may encounter the situation of php exporting excel at work. There are many methods on the Internet, but there is a very simple method, which I will not mention. If you are interested, you can search it. But this method does not really generate an excel table. If you want to actually generate an excel table, you have to use the phpExcel class. I will give the download address at the end of the article. How to use it after downloading? Here is a detailed source code example:

date_default_timezone_set("Etc/GMT-8"). //Set the time zone
header('Content-Type: text/html; charset =utf-8'); //Set the web page encoding method, preferably utf-8
require_once './Classes/PHPExcel.php'; //Set the path according to the path of your actual project
$objPHPExcel = new PHPExcel( ; );
mysql_select_db ("
test
",$conn); ); //Query sql statement
/*--------------Set header information------------------*/$objPHPExcel- & gt; setActiveSheetindex (0) -& gt; setCellValue ('a1', 'ID number') -& gt; setCellvalue ('b1', 'product name') -& gt; setCellvalue (' C1 ',' Item Number ')                                                       - ​ /* ---------------Start extracting information from the database and inserting it into the Excel table------------------*/
$i=2; /Define an i variable, the purpose is 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 the characters and convert the Chinese characters of GB2312 in the database into UTF-8 format
                                                           - >setCellValue("A".$i, $rs[0]) //Fill in data into the cell
        ->setCellValue("B".$i, $rm) //Because my column is in Chinese So coded 面-& gt; setCellValue ("c". $ I, $ rs [2])
- & gt; setCellValue ("d". $ I, $ rs [3]) ("E".$i, $rs[4])
                                                                                                                                                                                                                                     -------The following is to set other 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. Don't write it in excel5 or it will be garbled.
//If the code is still garbled, check the encoding method of your own data
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


//The above is the above data through the writing function of PHPExcel_IOFactory

$outputFileName = "Test.xls";

//Header your browser and tell it to force download instead of in Files running 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 , 26 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 the page
$objWriter->save(' php://output'); //Output to the browser
?>

This link is my source code, http://download.csdn.net/detail/luosisan/8255551. Including the phpexcel class I mentioned above. I’m new to PHP, so I hope you don’t find anything wrong.

The above introduces PHP to export excel2007 table, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template