phpexcel导出excel表简单入门示例

先下载PHPEXCEL类文件,放在class目录下面,然后新建一个index.php文件,内容如下

<?php

error_reporting(E_ALL);

ini_set('display_errors', TRUE);

ini_set('display_startup_errors', TRUE);

 

if (PHP_SAPI == 'cli')

die('This example should only be run from a Web Browser');

 

/** Include PHPExcel */

require_once 'class/PHPExcel.php';

 

// Create new PHPExcel object

$objPHPExcel = new PHPExcel();

 

// Set document properties

$objPHPExcel->getProperties()->setCreator("duchengjiu")

->setLastModifiedBy("duchengjiu")

->setTitle("Office 2007 XLSX userTable")

->setSubject("Office 2007 XLSX userTable")

->setDescription("Test userTable for Office 2007 XLSX.")

->setKeywords("office 2007")

->setCategory("userTable");

 

//连接数据库,获取数据库中的信息

mysql_connect('localhost','root','root');

mysql_select_db('test');

$result = mysql_query("select * from user");

$i = 1;

while(list($id, $username, $password) = mysql_fetch_row($result)){

// Add some data

$objPHPExcel->setActiveSheetIndex(0)

            ->setCellValue('A'.$i, $id)

            ->setCellValue('B'.$i, $username)

            ->setCellValue('C'.$i, $password);

$i++;

}

$objPHPExcel->getActiveSheet(0)->setTitle('Simple');

$objPHPExcel->setActiveSheetIndex(0);

 

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="01simple.xls"');

header('Cache-Control: max-age=0');

 

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

$objWriter->save('php://output');

exit;

?>


你可能感兴趣的:(PHP,Excel,phpexcel)