thinkphp5 導出excel

連接: https://pan.baidu.com/s/1sm7pcCiuDgYIUKeJMHRWKg 提取碼: bcis php

public function expExcel()  
{  
    vendor("PHPExcel.PHPExcel");  
    $list=db('member')->field('*')->select();  
    $objPHPExcel = new \PHPExcel();
    $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
    //設置sheet名稱
    $sheets=$objPHPExcel->getActiveSheet()->setTitle('demo');
    //設置sheet列頭信息
    $objPHPExcel->setActiveSheetIndex()->setCellValue('A1', 'id')->setCellValue('B1', 'name')->setCellValue('C1', 'image');
    $i=2;
    foreach($list as $v){
        //設置單元格的值
        $sheets=$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$v['id']);
        $sheets=$objPHPExcel->getActiveSheet()->setCellValue('B'.$i,$v['name']);
        $sheets=$objPHPExcel->getActiveSheet()->setCellValue('C'.$i,$v['image']);
        $i++;
    }
    //總體設置字體和字體大小
    $objPHPExcel->getDefaultStyle()->getFont()->setName( 'Arial');
    $objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
    // $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); //單元格寬度自適應
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); //設置列寬度
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); //設置列寬度
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); //設置列寬度
    $objPHPExcel->getActiveSheet()->getStyle('B3')->getFont()->setBold(true); //設置單元格字體加粗
    // 輸出Excel表格到瀏覽器下載
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="demo.xls"'); //excel表格名稱
    $objWriter->save('php://output');     // 下載文件

    
    // $objWriter->save('./public/export/test.xls');        // 保存到你指定的路徑中
}
相關文章
相關標籤/搜索