美好的一天,我很难找到如何使用codeigniter在PHPExcel中的每个值之前设置列名。由于我的PHPExcel工作从数据库中获取值,我的问题是如何设置每列的列名。
当前输出图片:
我的代码:
//load our new PHPExcel library
$this->load->library('excel');
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('Users list');
// Trying to set a column name
$this->excel->getActiveSheet()->SetCellValue('A1', 'ID');
$this->excel->getActiveSheet()->SetCellValue('B1', 'FIRST NAME');
$this->excel->getActiveSheet()->SetCellValue('C1', 'LAST NAME');
$this->excel->getActiveSheet()->SetCellValue('D1', 'EMAIL');
$this->excel->getActiveSheet()->SetCellValue('E1', 'TIME');
// get all users in array formate
$users = $this->User_info->getallusers();
// read data to active sheet
$this->excel->getActiveSheet()->fromArray($users);
$filename='just_some_random_name.xlsx'; //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, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
ob_end_clean();
$objWriter->save('php://output');
我希望输出看起来像这样
我看到的另一个问题是每列的宽度。你能帮我解决一下吗?
答案 0 :(得分:3)
$this->excel->getActiveSheet()->fromArray($users);
fromArray()
方法的默认左上角单元格是单元格A1
,它会覆盖已在单元格A1
中的所有内容....如果您想避免覆盖您已在第1行设置的标题,然后您需要告知fromArray()
从第2行开始,因此左上角的单元格将为A2
$this->excel->getActiveSheet()->fromArray($users, null, 'A2');
答案 1 :(得分:2)
您可以尝试使用此解决方案导出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('Users list');
$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()->getStyle("A1:E1")->applyFromArray(array("font" => array("bold" => true)));
$this->excel->setActiveSheetIndex(0)->setCellValue('A1', 'ID');
$this->excel->setActiveSheetIndex(0)->setCellValue('B1', 'FIRST NAME');
$this->excel->setActiveSheetIndex(0)->setCellValue('C1', 'LAST NAME');
$this->excel->setActiveSheetIndex(0)->setCellValue('D1', 'EMAIL ADDRESS');
$this->excel->setActiveSheetIndex(0)->setCellValue('E1', 'TIME');
// get all users in array formate
$this->excel->getActiveSheet()->fromArray($users, null, 'A2');
// read data to active sheet
$this->excel->getActiveSheet()->fromArray($users);
$filename='just_some_random_name.xlsx'; //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, 'Excel2007');
//force user to download the Excel file without writing it to server's HD
ob_end_clean();
$objWriter->save('php://output');
您可以使用 applyFromArray 为标题列添加更多样式。