CodeIgniter Forums
PHP export to Excel - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP export to Excel (/showthread.php?tid=42572)



PHP export to Excel - El Forum - 06-11-2011

[eluser]Unknown[/eluser]
I was written code same below but given error . can any body help me.

$this->load->plugin('PHPExcel');
$this->load->plugin('PHPExcel/IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

$objPHPExcel->setActiveSheetIndex(0);

// Field names in the first row
$fields = $query->list_fields();
$col = 0;
for($i=0;$i<count($fields);$i++)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field[$i]);
$col++;
}
/*foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}*/

// Fetching the table data
$row = 2;
foreach($query->result_array() as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
$col++;
}

$row++;
}

$objPHPExcel->setActiveSheetIndex(0);

$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');

$objWriter->save('php://output');