[英]Export mysql database to excel
I have been trying to export the content of mysql database to excel using the script below to no avail. 我一直试图使用下面的脚本将mysql数据库的内容导出到excel无济于事。 I must be doing somethign wrong:
我一定是做错了一些:
$getExcel = "SELECT name, age, course, city FROM person";
$res = mysql_query($getExcel);
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();
if(!$res){
die("Error");
}
$col = 0;
$row = 0;
while($row = mysql_fetch_assoc($res)) {
foreach($row as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
// 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\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('record.xlsx');
What I'm I doing wrong please? 我做错了什么?
Your issue is, likely, that you're using the variable $row
in two different contexts ... try this: 您的问题可能是您在两个不同的上下文中使用变量
$row
...尝试这样:
$row = 1;
while($mrow = mysql_fetch_assoc($res)) {
$col = 0;
foreach($mrow as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.