I have data that includes a day column. There are upwards of 400 row for each day.
I'd like to export each day's data in to their own Excel sheet.
To group the data I'm using SELECT * FROM mydata GROUP BY DATE(mydata.day);
.
How can I get all that grouped data and do the insert in to the excel doc, with each dates data in its own sheet? Right now my attempt looks like it will only insert data for one day.
Essentially, I'll need a for each
to go through each grouped data.. as in, for each day, loop through and output the data in its sheet.
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
@mysql_select_db($db['name']);
$query = mysql_query('SELECT * FROM mydata GROUP BY DATE(mydata.day)');
)
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Create a new worksheet
$objPHPExcel->createSheet();
$sheet=0; //this will need to be outside a loop so I can $sheet++ to set the new ActiveSheetIndex
// Set excelrow to 2 so we skip the line of headings
$excelrow=2;
while($row = mysql_fetch_array($query)){
// Set sheet index
$objPHPExcel->setActiveSheetIndex($sheet);
// Set the sheet title
$objPHPExcel->getActiveSheet()->setTitle($row['day']);
// Set sheet headings
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Name');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Email');
// Get the data
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$excelrow, $row['name']);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$excelrow, $row['email']);
$excelrow++;
}
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');