在codeigniter中生成Excel工作表

时间:2016-06-23 18:28:57

标签: excel codeigniter

我想生成一个excel表,但是我只获得没有标题的值,任何人都可以帮助我如何使用样式赋予列值,这是我的控制器

public function generateexcel()
{
    require_once("PHPExcel/Classes/PHPExcel.php");
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(12);
        $objRichText = new PHPExcel_RichText();
        $objRichText->createText("Students");
        $objRichText = new PHPExcel_RichText();
        $objRichText->createText("Student Details");
        $students = $this->student->getstudents;
        $objPHPExcel->getActiveSheet()->fromArray($students); 
        $filename = "students.xls";
        $objPHPExcel->getActiveSheet()->setTitle('Students');
        $objPHPExcel->setActiveSheetIndex(0);
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header("Content-Disposition: attachment;filename=\"".$filename."\";");
        header("Cache-Control: max-age=0");
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');


}

1 个答案:

答案 0 :(得分:0)

如果你想用db中的信息创建一个excel文件,试试这个

$this->load->library('excel');

$filename = 'tab';
    $title = 'title name';
    $file = $filename . '-' . $name . '.xls'; //save our workbook as this file name
    $query = $his->model->function($params);

    header('Content-Type: application/vnd.ms-excel'); //mime type
    header('Content-Disposition: attachment;filename="'.$file.'"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache

    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle($filename);
    $this->excel->getActiveSheet()->setCellValue('A1', $title);
    $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
    $this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
    $this->excel->getActiveSheet()->mergeCells('A1:' . $cell . '1');
    $this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    $this->excel->getActiveSheet()->setCellValue('A2', 'Subtitulo 1');
    $this->excel->getActiveSheet()->setCellValue('B2', 'Subtitulo 2');
    $this->excel->getActiveSheet()->setCellValue('C2', 'Subtitulo 3');
    $this->excel->getActiveSheet()->setCellValue('D2', 'Subtitulo 4');
    $this->excel->getActiveSheet()->setCellValue('E2', 'Subtitulo 5');
    $this->excel->getActiveSheet()->getStyle('A2:E2')->getFont()->getColor()->setRGB('555555'); 
    $this->excel->getActiveSheet()->getStyle('A2:E2')->getFont()->setSize(10);
    $this->excel->getActiveSheet()->getStyle('A2:E2')->getFont()->setBold(true);

    $i = 3;
    foreach($query as $item){
        $this->excel->getActiveSheet()->setCellValue('A'.$i, $item['cell1']);
        $this->excel->getActiveSheet()->setCellValue('B'.$i, $item['cell2']);
        $this->excel->getActiveSheet()->setCellValue('C'.$i, $item['cell3']);
        $this->excel->getActiveSheet()->setCellValue('D'.$i, number_format($item['cell4'], 0, ',', '.'));
        $this->excel->getActiveSheet()->setCellValue('E'.$i, date('d M Y h:i a', strtotime($item['date1'])));

        //$this->excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $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);
        $i++;
    }

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