使用CI在excel中导出数据

我正试图导出Excel表格中的数据。 我已经成功导出的数据,但问题是,我的数据来自数据库的数组forms,我不希望所有的数组显示我只想select性的数据发送到Excel表帮助请。

{ //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'); } 

这是我现在所做的。

尝试下面的代码。 我在你的代码中做了一些改变,
//在$ row ['']variables中传递表名字段的名称

 { //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'); }