码迷,mamicode.com
首页 > Web开发 > 详细

【PHPExcel实例】 php 导出 excel 实例

时间:2015-09-01 21:29:23      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:

CREATE TABLE `person` (
`number` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`birthday` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO person values (1,‘xiaofei‘,‘20001111‘);
INSERT INTO person values (2,‘xiaopi‘,‘20111111‘);

 

<?php
/**
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 * @version    ##VERSION##, ##DATE##
 */
/** Error reporting */
error_reporting(E_ALL);
ini_set(‘display_errors‘, TRUE);
ini_set(‘display_startup_errors‘, TRUE);
date_default_timezone_set(‘Europe/London‘);
if (PHP_SAPI == ‘cli‘)
    die(‘This example should only be run from a Web Browser‘);
/** Include PHPExcel */
require_once dirname(__FILE__) . ‘/../common/PHPExcel.php‘;
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                             ->setLastModifiedBy("Maarten Balliauw")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue(‘A1‘, ‘ID‘)
            ->setCellValue(‘B1‘, ‘姓名!‘)
            ->setCellValue(‘C1‘, ‘生日‘);
// add mysql data, UTF-8
$con = mysql_connect("localhost","root","root");
if (!$con){die(‘Could not connect: ‘ . mysql_error());}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM person");
$i = 1;
while($row = mysql_fetch_array($result)){
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue(‘A‘.$i, $row[‘number‘])
                ->setCellValue(‘B‘.$i, $row[‘name‘])
                ->setCellValue(‘C‘.$i, $row[‘birthday‘]);
    $i++;
}

mysql_close($con);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle(‘示例‘);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header(‘Content-Type: application/vnd.ms-excel‘);
header(‘Content-Disposition: attachment;filename="01simple.xls"‘);
header(‘Cache-Control: max-age=0‘);
// If you‘re serving to IE 9, then the following may be needed
header(‘Cache-Control: max-age=1‘);
// If you‘re serving to IE over SSL, then the following may be needed
header (‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); // Date in the past
header (‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘).‘ GMT‘); // always modified
header (‘Cache-Control: cache, must-revalidate‘); // HTTP/1.1
header (‘Pragma: public‘); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
$objWriter->save(‘php://output‘);
exit;

 

【PHPExcel实例】 php 导出 excel 实例

标签:

原文地址:http://www.cnblogs.com/bushe/p/4776874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!