在phpexcel中为列添加标题时,它会隐藏结果的第一行

时间:2018-07-13 08:59:05

标签: php codeigniter phpexcel

  $users = $query->result_array();
    $this->excel->getActiveSheet()->SetCellValue('A1', 'Name');

    $this->excel->getActiveSheet()->SetCellValue('B1', 'Email');
    $this->excel->getActiveSheet()->SetCellValue('C1', 'Gender');
    $this->excel->getActiveSheet()->SetCellValue('D1', 'DOB');
    $this->excel->getActiveSheet()->SetCellValue('E1', 'Class');
    $this->excel->getActiveSheet()->SetCellValue('F1', 'School Name');
    $this->excel->getActiveSheet()->SetCellValue('G1', 'City');
    $this->excel->getActiveSheet()->SetCellValue('H1', 'State');
    $this->excel->getActiveSheet()->SetCellValue('I1', 'Primary Contact');
    $this->excel->getActiveSheet()->SetCellValue('J1', 'Secondary Contact');
    $this->excel->getActiveSheet()->SetCellValue('K1', 'Teacher Contact');
    $this->excel->getActiveSheet()->fromArray($users);



    $filename = 'userlist.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


    $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');


    $objWriter->save('php://output');

我正在CodeIgniter上使用此代码。它工作正常,但我唯一无法解决的问题 它隐藏数据库输出的第一行。如何将数据库结果转换为从A2开始。

1 个答案:

答案 0 :(得分:1)

您可以从单元格编号开始引用单元格编号

$current_cell = 2;
foreach ($cell_val as $cell) {
   $objPHPExcel->getActiveSheet()->fromArray($cell, null, 'A'.$current_cell);
   $current_cell++;
}