Saturday, January 9, 2016

[sugarcrm] Export data into excel 2007 using PHPExcel

How can we export data to excel 2007? You can use library PHPExcel

Step 1: You need to download PHPExcel from https://phpexcel.codeplex.com/. i used PHPExcel1.8.0 version.
Step 2: Copy this library into custom\include\.
Step 3: On module which you want to use this library for export to excel, you need include it

include('custom/include/PHPExcel1.8.0/PHPExcel.php');
Step 4: Developing data to excel 2007. Please parammeter of createWriter method. the second param is excel version. If you want to export data excel 2007 you need to set Excel2007.
More information,you can read at https://github.com/PHPOffice/PHPExcel/wiki

// Send and close the file
$workbook->close();
$objPHPExcel = PHPExcel_IOFactory::load($path);
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Student Data.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
unlink($path);
exit;


No comments: