基于YII框架的PHPExcel导出

 public function actionOutexceInventoryList(){
       ob_end_clean();
       ob_start();

       /** PHPExcel */
       //Yii::import('application.vendors.*');
       include_once(dirname(dirname(__FILE__)).'/extensions/PHPExcel.php');
       include_once(dirname(dirname(__FILE__)).'/extensions/PHPExcel/Writer/Excel2007.php');
       $objPHPExcel = new PHPExcel();

       $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
           ->setLastModifiedBy("Maarten Balliauw")
           ->setTitle("Office 2007 XLSX Test Document")
           ->setSubject("Office 2007 XLSX Test Document")
           ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
           ->setKeywords("office 2007 openxml php")
           ->setCategory("Test result file");

     //  $objPHPExcel->setActiveSheetIndex(0)首行字段名称
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '院内编码');
         $objPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
          $objPHPExcel->getActiveSheet() ->setCellValue('C1', '厂商');
           $objPHPExcel->getActiveSheet() ->setCellValue('D1', '规格');
          $objPHPExcel->getActiveSheet()  ->setCellValue('E1', '使用科室');
          $objPHPExcel->getActiveSheet()  ->setCellValue('F1', '启用日期');
           $objPHPExcel->getActiveSheet() ->setCellValue('G1', '单价');
           $objPHPExcel->getActiveSheet() ->setCellValue('H1', '资金来源');
           $objPHPExcel->getActiveSheet() ->setCellValue('I1', '盘点状态');
       //   $objPHPExcel->getActiveSheet()  ->setCellValue('J1', '存放地点');
       //   $objPHPExcel->getActiveSheet()  ->setCellValue('K1', '盘点结果');
       //居中,颜色
       $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
       $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1:I1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);

       $param = array();
       $stocks_id = Yii::app()->request->getParam('stocks_id');
       $criteria = new CDbCriteria();
       $criteria->addCondition( 'stocks_id='.$stocks_id);
       $param['stockList'] = Stocksproduct::model()->findAll($criteria);
       $param['stocks_id'] = $stocks_id;
       foreach( $param['stockList'] as $key=>$val ){
           $key =$key+2;
           if($val['status']==1){
               $status = "未盘点";
           }elseif( $val['status']==2 ){
               $status = "盘亏";
           }elseif( $val['status']==3 ){
               $status = "盘实";
           }elseif( $val['status']==4 ){
               $status = "盘盈";
           }
           $card = FormatHelper::getCardInfo( $val['cardId'] );
           //设置单元格格式为文本格式
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("A$key", $card->assetsId,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("B$key", $card->assetsName,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("C$key", $card->manufacturer,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("D$key", $card->modelSpecifications,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("E$key", $card->name_useOffice,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("F$key", $card->useDate,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("G$key", $card->assetValue,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("H$key", $card->suggest_fundingSources,PHPExcel_Cell_DataType::TYPE_STRING);
           $objPHPExcel->getActiveSheet()->setCellValueExplicit("I$key", $status,PHPExcel_Cell_DataType::TYPE_STRING);
          // $objPHPExcel->getActiveSheet()->setCellValueExplicit("J$key", $card->storageLocation,PHPExcel_Cell_DataType::TYPE_STRING);
         //  $objPHPExcel->getActiveSheet()->setCellValueExplicit("k$key",  $status ,PHPExcel_Cell_DataType::TYPE_STRING);

       }

       $K ="I".$key;
       //设置从第二行起全部左对齐
        $objPHPExcel->setActiveSheetIndex(0)->getStyle("A2:$K")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
       //设置宽度
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(15);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(15);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(25);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(15);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(15);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(20);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(20);
       $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(20);
     //  $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(25);
     //  $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);

       //设置边框
      // $objPHPExcel->getActiveSheet()->getStyle("A1:$K" )->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      // $objPHPExcel->getActiveSheet()->getStyle("A1:$K" )->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
      // $objPHPExcel->getActiveSheet()->getStyle("A1:$K" )->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
       //$objPHPExcel->getActiveSheet()->getStyle("A1:$K" )->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

       $objPHPExcel->getActiveSheet()->setTitle('盘点单');
       $objPHPExcel->setActiveSheetIndex(0);
//          $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//          $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
       $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

       header("Pragma: public");
       header("Expires: 0");
       //header('Content-Type: application/vnd.ms-excel;charset=utf8');
       header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
       header("Content-Type:application/force-download");
       header("Content-Type:application/vnd.ms-execl");
       header("Content-Type:application/octet-stream");
       header("Content-Type:application/download");
       $fireName = $stocks_id.'-盘点单';
       header("Content-Disposition:attachment;filename=$fireName.xls");
       header("Content-Transfer-Encoding:binary");
       $objWriter->save("php://output");

       Yii::app()->end();
       spl_autoload_register(array('YiiBase','autoload'));
   }


你可能感兴趣的:(PHP)