编程语言
首页 > 编程语言> > PHP导出Excel

PHP导出Excel

作者:互联网

public function dcmwexport_excel() {
       
         set_time_limit(0);
            $page=1;//页数
            $limit=1000;//每页条数
            
             $list=(new \app\common\model\XXX())->XXX($where,$page,$limit);
		//引入文件
             include app()->getRootPath() . "/extend/PHPExcel/PHPExcel.php";
             include app()->getRootPath() . "/extend/PHPExcel/PHPExcel/Reader/Excel5.php";
             include app()->getRootPath() . "/extend/PHPExcel/PHPExcel/IOFactory.php";
             $PHPExcel = new \PHPExcel();
             $PHPSheet = $PHPExcel->getActiveSheet();
             

             $PHPSheet->setTitle("会员卡表"); //给当前活动sheet设置名称
             $PHPSheet->setCellValue("A1","XX")
                 ->setCellValue("B1","XX")
                 ->setCellValue("C1","XXX")
                 ->setCellValue("D1","XX数")
                 ->setCellValue("E1","是否X")
                 ->setCellValue("F1","激活X")
                 ->setCellValue("G1","提交时间");
         //设置单元格宽度
            $PHPSheet->getColumnDimension("A")->setWidth(30);
            $PHPSheet->getColumnDimension("B")->setWidth(30);
            $PHPSheet->getColumnDimension("C")->setWidth(18);
            $PHPSheet->getColumnDimension("D")->setWidth(10);
            $PHPSheet->getColumnDimension("E")->setWidth(10);
            $PHPSheet->getColumnDimension("F")->setWidth(10);
            $PHPSheet->getColumnDimension("G")->setWidth(20);

             $i=2;
             foreach ($list['data'] as $k=>$v){
                
                 $PHPSheet->setCellValue("A".$i,' '.$v['X'])
                     ->setCellValue("B".$i,' '.$v['X'])
                     ->setCellValue("C".$i,$v['X'])
                     ->setCellValue("D".$i,$v['X'])
                     ->setCellValue("E".$i,$v['X'])
                     ->setCellValue("F".$i,$v['X'])
                     ->setCellValue("G".$i,$v['X']);//表格数据
                 $i++;

             }
             $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
             header('Content-Disposition: attachment;filename="会员卡表.xlsx"');
             header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
             $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
             exit();

    }```

标签:PHP,getColumnDimension,Excel,app,导出,PHPSheet,PHPExcel,setCellValue,setWidth
来源: https://blog.csdn.net/weixin_43018356/article/details/115244446