在 codeigniter 中使用 PHPExcel 设置列名和列宽

set column name and column width using PHPExcel in codeigniter

美好的一天,我正在努力寻找如何使用 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');

我希望输出看起来像这样

我看到的另一个问题是每列的宽度。你能帮我修好吗?

您可以尝试使用此解决方案进行导出 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 为标题栏添加更多样式。

$this->excel->getActiveSheet()->fromArray($users);

fromArray() 方法的默认 top-left 单元格是单元格 A1,它将覆盖单元格 A1 中已有的任何内容...如果您想要避免覆盖您已经在第 1 行中设置的 headers,那么您需要告诉 fromArray() 从第 2 行开始,因此 top-left 单元格将为 A2

$this->excel->getActiveSheet()->fromArray($users, null, 'A2');