yii2.0 利用Excel类做导入导出

时间:2022-10-20 20:28:08
1、在 common 目录下 创建一个 components 将 Classes目录(改名为PHPExcel)和PHPExcel.php 放在新创建的目录下。再在 components 下创建一个CommonFunc.php:           <?php               namespace common\components;               use yii;               class CommonFunc{                               public static function expor(){                                          require(Yii::getAlias("@common")."/components/PHPExcel.php");                                          require(Yii::getAlias("@common")."/components/PHPExcel/Writer/Excel2007.php");                                          $obj=new \PHPExcel();                                          $objSheet=$obj->getActiveSheet();           //获取当前sheet活动对象
                                         $objSheet->setTitle("标题");                                          $objSheet->setCellValue('A1','姓名')->setCellValue('B1','年龄');                                          $objWriter= new \PHPExcel_Writer_Excel2007($obj);                                          header("Pragma:public");                                          header("Content-Type:application/x-msexecl;name=\"{$filename}.xls\"");                                          header("Content-Disposition:inline;filename=\"{$filename}.xls\"");                                          $objWriter->save("php://output");                               }               }                        ?> 2、任意控制器下:          use common\components\CommonFunc;          CommonFunc::exportData();                                    //调用