phpexcel实现excel导出

   //使用前记得安装phpexcel
   //composer require phpoffice/phpexcel  
  
    use PHPExcel_IOFactory;
    use PHPExcel;  


    $PHPExcel = new PHPExcel(); //实例化PHPExcel类
    $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
    $PHPSheet->setTitle('Sheet'); //给当前活动sheet设置名称
    $i = 1;
    $PHPSheet->setCellValue('A'.$i,'today rank')
             ->setCellValue('B'.$i,'domain');
    foreach ($result as $key => $item) {
        $i++;
        $PHPSheet->setCellValue('A'.$i, $item['today_rank'])
             ->setCellValue('B'.$i, $item['domain']);
    }
    $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel,'Excel5');
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
    header('Content-Disposition: attachment;filename="xxxx.xlsx"');
    header('Cache-Control: max-age=0');//禁止缓存
    $PHPWriter->save("php://output");

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章