PHPExcel从数据库导出数据

话不多说,直接上代码。

$this->load->library('PHPExcel');
        $this->load->library('PHPExcel/IOFactory');
        $objExcel = new PHPExcel();
//获得当前sheet操作对象
        $objSheet = $objExcel->getActiveSheet();
//设置sheet名称
        $objSheet->setTitle('mysheet');
        $objSheet->setCellValue('A1','ID')
                ->setCellValue('B1','标题')
                ->setCellValue('C1','时间')
                ->setCellValue('D1','作者');
        $newlist = $this->newsmodel->getList();
        $i = 2;
        foreach($newlist as $key=>$val)
        {
            $objSheet->setCellValue('A'.$i,$val['newsId'])
                ->setCellValue('B'.$i,$val['title'])
                ->setCellValue('C'.$i,$val['createTime'])
                ->setCellValue('D'.$i,$val['editor']);
            $i++;
        }
        $objWriter = IOFactory::createWriter($objExcel, 'Excel2007');
//保存数据
        $objWriter->save('myexcel4.xlsx');
    }

 

你可能感兴趣的:(PHPExcel从数据库导出数据)