有没有办法使用PHPExcel将多个查询的结果导出到单个Excel工作表?这是我到目前为止所使用的一个查询:
<?php
// connection with the database
$dbhost = "localhost";
$dbuser = "admin";
$dbpass = "password";
$dbname = "databasename";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
// require the PHPExcel file
require 'classes/PHPExcel.php';
// simple query
$query = "SELECT denisCheckedby, denisStatus, denisDate, denisNotes FROM tblDenis ORDER by denisID DESC";
$headings = array('Checked By', 'Successfull?','Date','Notes');
if ($result = mysql_query($query) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('Backup Check');
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 2;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
// Freeze pane so that the heading line will not scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// Set column widths
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
// Set fonts
$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Backup Check.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
echo 'a problem has occurred... no data retrieved from the database';
?>
如何使用多个查询执行相同的操作?例如,我的第二个查询是:
"SELECT curtisCheckedby, curtisStatus, curtisDate, curtisNotes FROM tblCurtis ORDER by curtisID DESC";
非常感谢您的帮助。