zoukankan      html  css  js  c++  java
  • 【PHPExcel实例】 php 导出 excel 实例

    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;
  • 相关阅读:
    链接
    Java垃圾收集算法1
    Java基础知识常见面试题汇总第一篇
    Java基础知识
    线程间通信
    System.Span, System.Memory,还有System.IO.Pipelines
    微服务构件
    定位恶意软件
    Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)
    Spring MVC-处理程序映射(Handler Mapping)-控制器类名称处理程序映射(Controller Class Name Handler Mapping)示例(转载实践)
  • 原文地址:https://www.cnblogs.com/bushe/p/4776874.html
Copyright © 2011-2022 走看看