I have been trying to export the content of mysql database to excel using the script below to no avail. 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?
htmlfile in your code with atablein it, and save it as.xls, then open it with excel. it's a dirty dirty hack but it works if all you need to do is export some data to excel. It won't an entire whole system...