I am trying to export data in excel sheet.
i have successfully exported the data but problem is that my data comes in form of array from database and i dont want all of the array to be displayed i want only selective data to be sent to excel sheet
help please.
{
//load our new PHPExcel library
$this->load->library('Excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('Attendance Report');
$this->excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
$this->excel->getActiveSheet()->getStyle("A1:P1")->applyFromArray(array("font" => array("bold" => true)));
$this->excel->setActiveSheetIndex(0)->setCellValue('A1', 'Date (Day)');
$this->excel->setActiveSheetIndex(0)->setCellValue('B1', 'Worked Hour');
$this->excel->setActiveSheetIndex(0)->setCellValue('C1', 'In Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('D1', 'In Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('E1', 'Out Date');
$this->excel->setActiveSheetIndex(0)->setCellValue('F1', 'Out Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('G1', 'Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('H1', 'Difference');
$this->excel->setActiveSheetIndex(0)->setCellValue('I1', 'In Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('J1', 'In Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('K1', 'Out Date');
$this->excel->setActiveSheetIndex(0)->setCellValue('L1', 'Out Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('M1', 'Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('N1', 'Difference');
$this->excel->setActiveSheetIndex(0)->setCellValue('O1', 'Total OT');
$this->excel->setActiveSheetIndex(0)->setCellValue('P1', 'Remark');
$data=$this->mainModel->exportAttendanceReport();
// get all users in array formate
$this->excel->getActiveSheet()->fromArray($data, null, 'A2');
$filename='Attendance Report.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
}
here is what i have done upto now.
Try Following code. I am done some changes in your code ,
// Pass name of your tables field name in $row[''] variable
{
//load our new PHPExcel library
$this->load->library('Excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('Attendance Report');
$this->excel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('J')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('K')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('L')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('M')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('N')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('O')->setAutoSize(true);
$this->excel->getActiveSheet()->getColumnDimension('P')->setAutoSize(true);
$this->excel->getActiveSheet()->getStyle("A1:P1")->applyFromArray(array("font" => array("bold" => true)));
$this->excel->setActiveSheetIndex(0)->setCellValue('A1', 'Date (Day)');
$this->excel->setActiveSheetIndex(0)->setCellValue('B1', 'Worked Hour');
$this->excel->setActiveSheetIndex(0)->setCellValue('C1', 'In Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('D1', 'In Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('E1', 'Out Date');
$this->excel->setActiveSheetIndex(0)->setCellValue('F1', 'Out Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('G1', 'Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('H1', 'Difference');
$this->excel->setActiveSheetIndex(0)->setCellValue('I1', 'In Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('J1', 'In Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('K1', 'Out Date');
$this->excel->setActiveSheetIndex(0)->setCellValue('L1', 'Out Time');
$this->excel->setActiveSheetIndex(0)->setCellValue('M1', 'Remarks');
$this->excel->setActiveSheetIndex(0)->setCellValue('N1', 'Difference');
$this->excel->setActiveSheetIndex(0)->setCellValue('O1', 'Total OT');
$this->excel->setActiveSheetIndex(0)->setCellValue('P1', 'Remark');
$data=$this->mainModel->exportAttendanceReport();
// get all users in array formate
$n = 2;
if(!empty($data))
{
foreach ($data as $row)
{
// Pass name of your tables field name in following $row[''] variable
$this->excel->getActiveSheet()->setCellValue('A'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('B'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('C'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('D'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('E'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('F'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('G'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('H'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('I'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('J'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('K'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('L'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('M'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('N'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('O'.$n, $row['field_name']);
$this->excel->getActiveSheet()->setCellValue('P'.$n, $row['field_name']);
$n++;
}
}
$filename='Attendance Report.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
}