我正在尝试在Excel工作表中导出数据。 我已成功导出数据,但问题是我的数据来自数据库的数组形式,我不希望显示所有数组我只想将选择性数据发送到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');
}
这是我现在所做的事情。
答案 0 :(得分:2)
尝试以下代码。我对你的代码做了一些修改,
//在$ row ['']变量
{
//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');
}
答案 1 :(得分:0)
尝试以下代码
控制器
public function users_export(){
$serarch_rec = array();
$method = $this->input->server("REQUEST_METHOD");
if( $method == 'POST'){
$serarch_rec = $this->input->post();
}
$filename = 'users_'.date('Ymd').'.xls';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/xls; ");
$subscribers = $this->admin_model->get_records( $serarch_rec );
$file = fopen('php://output', 'w');
fwrite($file, "<style> th, td{ border: solid 1px #ddd; }</style>");
fwrite($file, "<table> <thead>");
fwrite($file, "<tr> <th>Date</th> <th>Name</th> <th>Mobile</th> <th>Email</th> <th>Country</th> <th>Plan</th> <th>About</th> </tr>");
fwrite($file, "</thead> <tbody>");
foreach ($subscribers as $key => $val) {
fwrite($file, "<tr> <th>$val->create_at</th> <th>$val->name</th> <th>$val->mobile</th> <th>$val->email</th> <th>$val->country</th> <th>$val->plan</th> <th>$val->about</th> </tr>");
}
fwrite($file, "</tbody> <table>");
fclose( $file );
exit();
}
型号:
public function get_records($serach_rec){
$this->db->select('create_at, name, mobile, email, country, plan, about')->order_by("create_at", "DESC");
$this->db->like($serach_rec);
$records = $this->db->get_where("users");
return $records->result();
}
HTML:
<a href="<?= base_url('users_export') ?>" class="btn btn-success "><i class="fa fa-file-excel-o"></i></a>