I find that downloading excel sheets using PHPExcel works once when using post. Then I have to reload the page. Whereas with a get url I can download the file multiple times, without reloading. I assume the issue is that once I post data I need to get a new csrf hash, which requires a reload from the server. Does anyone know a workaround for this? Is there a way to auto-reload the page once the file is saved to output?
Controller method
The last lines of the excel_report->report method
Code:
<?php echo form_open('reports/people', 'class="report_form"'); ?>
<input type="hidden" name="friend_filter" value="all" />
// this post works once and then gives a 404 if the button is pushed again
<input type="submit" name="download_report" value="Download" class="btn btn-primary" />
// this get works multiple times without having to reload the page
<?php echo anchor('reports/people' . '/' . 'all', 'Download', 'class="btn btn-primary" id="download_friends"');?>
Controller method
Code:
// method parameter accepting get data
public function people($friend_filter) {
// this line is used when the method accepts post data
//$friend_filter = $this->input->post('friend_filter');
$this->excel_report->report(
$this->people->table($friend_filter),
'people'
);
}
The last lines of the excel_report->report method
Code:
$filename = $report_name . '_' . date('Y_M_d_G_i') . '.xlsx';
// Define the mime type
header('Content-Type: application/vnd.ms-excel');
// Tell the browser the file name
header('Content-Disposition: attachment;filename="'.$filename.'"');
//no cache
header('Cache-Control: max-age=0');
// save it to the specified Excel format
// .xls file: Excel2003 (adjust the filename extension, also the header mime type)
// .xlsx file: Excel2007 (adjust the filename extension, also the header mime type)
// The $objWriter is created by calling a static (class) method on PHPExcel_IOFactory and
// passing in the PHPExcel instance.
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');