<?php
/**
* Phpexcel Generate Excel File
* @author: Firmy
* @desc supports arbitrary row and column data to generate Excel files without adding cell styles and alignment
*/
Header ("Connect-type:text/html;charset=utf-8");
$DSN = "Mysql:host=localhost;dbname=me";
$db =new PDO ($dsn, ' root ', ' 123 ', Array (pdo::mysql_attr_init_command=> ' SET NAMES \ ' utf8\ '));
$count = $db->query ("SELECT * from Test");
$data = $count->fetchall (PDO::FETCH_ASSOC);
Print_r ($res);d ie;
Require_once ' classes/phpexcel.php ';
Require_once ' classes/phpexcel/reader/excel2007.php ';
Require_once ' classes/phpexcel/reader/excel5.php ';
Include_once ' classes/phpexcel/iofactory.php ';
Create a new Phpexcel object
$objPHPExcel = new Phpexcel ();
$objProps = $objPHPExcel->getproperties ();
Set up the table header
$i = 0;
foreach ($data as $k = = $v) {
Print_r ($v);d ie;
if ($i <1) {
$ARRD =array_keys ($v);
Print_r ($ARRD);d ie;
$key = Ord ("A");
foreach ($arrd as $kk = = $VV) {
$colum = Chr ($key);
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $VV);
$key + = 1;
}
}
$i + +;
}
$column = 2;
$objActSheet = $objPHPExcel->getactivesheet ();
foreach ($data as $key = + $rows) {//Line write
$span = Ord ("A");
foreach ($rows as $keyName = + $value) {//Column write
$j = Chr ($span);
$objActSheet->setcellvalue ($j. $column, $value);
$span + +;
}
$column + +;
}
Download
REDIRECT output to a client Web browser (Excel2007)
$fileName = Iconv ("Utf-8", "gb2312", "Bao.xls");
Header ("content-disposition:attachment; Filename=\ "$fileName \" ");
Header (' cache-control:max-age=0 ');
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
$objWriter->save (' php://output '); Files are downloaded through the browser
Phpexcel Download (get data from database) sample code