php-reports icon indicating copy to clipboard operation
php-reports copied to clipboard

[Fixed] Excel Export

Open geo782 opened this issue 7 years ago • 0 comments

Hi, Im sharing my solution to make excel export to work, as i was getting a white screen. For fixing this im using phpspreadsheet, phpexcel is abandoned.

  1. cd folderofphpreportscript
  2. composer require phpoffice/phpspreadsheet
  3. cd classes/report_formats/ -----Where the old files for excel exist
  4. /var/www/html/ticket/phpreport/vendor/phpoffice/phpspreadsheet/bin/migrate-from-phpexcel
    -----Run the script to change old phpexcel commands to phpspreadsheet's commands If succesfull, you will get the following lines :
/var/www/html/ticket/phpreport/classes/report_formats/XlsReportBase.php converted
/var/www/html/ticket/phpreport/classes/report_formats/XlsReportFormat.php converted
/var/www/html/ticket/phpreport/classes/report_formats/XlsxReportFormat.php converted
  1. Make sure that XlsxReportFormat.php in classes/report_formats/ is like :
<?php
use PhpOffice\PhpSpreadsheet\Helper\Sample;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class XlsxReportFormat extends XlsReportBase {
	public static function display(&$report, &$request) {
		// First let set up some headers
		$spreadsheet = new Spreadsheet();
		$file_name = preg_replace(array('/[\s]+/','/[^0-9a-zA-Z\-_\.]/'),array('_',''),$report->options['Name']);
		//always use cache for Excel reports
		$report->use_cache = true;
		//run the report
		$report->run();
		if(!$report->options['DataSets']) return;
		$objPHPExcel = parent::getExcelRepresantation($report);
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="'.$file_name.'.xlsx"');
		header('Pragma: public');
		header('Expires: 0');
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
		$objWriter->save('php://output');
	}
}

geo782 avatar Aug 13 '18 05:08 geo782