header("Content-Type: text/html;charset=utf-8");
date_default_timezone_set('Asia/Shanghai');
$my_host = "127.0.0.1"; //主机
$my_user = "
xxxxxx"; //用户名
$my_pass = "
xxxxxx"; //密码
$my_data = "xxxxxx"; //MySQL库名
// Connect to MySQL
$mysqli = new mysqli($my_host, $my_user, $my_pass,$my_data);
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
//设置连接的字符集,也可以用$mysqli->query("SET NAMES UTF8");来设置
//$mysqli->query("SET NAMES UTF8");
$mysqli->set_charset("utf8");
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '
');
// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("wangxuwei")
->setLastModifiedBy("wangxuwei")
->setTitle("PHPExcel Test document")
->setSubject("PHPExcel Test document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
// Add some data
echo date('H:i:s') , " Add title" , EOL;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '学号')
->setCellValue('B1', '姓名')
->setCellValue('C1', '生日')
->setCellValue('D1', '性别')
->setCellValue('E1', '研究方向');
//执行一个简单查询,结果输出到excel文件
echo date('H:i:s') , " Add data from MySQL server " , EOL;
$result=$mysqli->query('select SNO,SNAME,BDATE,SEX,DIR from STUDENT');
$i=1;
while($row=$result->fetch_array(MYSQLI_BOTH))
{
$i++;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$row['SNO'])
->setCellValue('B'.$i,$row['SNAME'])
->setCellValue('C'.$i,$row['BDATE'])
->setCellValue('D'.$i,$row['SEX'])
->setCellValue('E'.$i,$row['DIR']);
}
// Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL;
$objPHPExcel->getActiveSheet()->setTitle('学生信息表');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel 2007 file
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'Files have been created in ' , getcwd() , EOL;
// Clean up
$result->close();
$mysqli->close();