导出到 excel xls 在实时服务器上不工作

Export to excel xls not working on live server

我正在尝试将 gridview 导出到 Yii 中的 excel xls。为此,我使用了 PHPExcel 扩展。它在本地主机上工作,但在实时服务器上不工作。

以下是我的模型函数:

public function downloadOrderReport($model){

        Yii::import('ext.phpexcel.XPHPExcel');

        $objPHPExcel= XPHPExcel::createPHPExcel();

        ini_set ( 'memory_limit', '150M' );

        $fileName=DOWNLOADFILENAME.'.xls'; // download file name

        // Add some data
        $objPHPExcel->setActiveSheetIndex(0);   

        $columnName=array("ID"=>"Order Number","AddedOn"=>"Order Date","DealerID"=>"Dealer Name","SubmittedByUserID"=>"Executive Name","OrderStatusID"=>"Order Status","TotalAmount"=>"Order Amount");  

        $col = 0;
        $row = 1;       
        foreach($columnName as $key=>$value) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
            $col++;
        }

        $criteria=new CDbCriteria;
        $criteria->with = array('dealer'=>array("select"=>"Name"),'submittedByUser'=>array("select"=>"Name"),'ApprovedOrRejectedByUser'=>array("select"=>"Name"),'orderStatus'=>array('select'=>'OrderStatus,ID'));
        $criteria->condition = "t.IsDeleted <> '".DELETED."'";

        if((isset($model->from_date) && trim($model->from_date) != "") && (isset($model->to_date) && trim($model->to_date) != "")){
            $criteria->condition .= ' AND t.AddedOn >="'.date("Y-m-d",strtotime($model->from_date)).'" AND t.AddedOn <="'.date("Y-m-d",strtotime($model->to_date)).'"';
        }

        $criteria->compare('t.ID',$model->ID);
        $criteria->compare('dealer.Name',$model->DealerID,true);
        $criteria->compare('submittedByUser.Name',$model->SubmittedByUserID,true);
        $criteria->compare('OrderStatusID',$model->OrderStatusID,true);
        $criteria->compare('ApprovedOrRejectedByUser.Name',$model->ApprovedOrRejectedByUserID,true);
        $criteria->compare('Remarks',$model->Remarks,true);
        $criteria->compare('TotalAmount',$model->TotalAmount);
        $criteria->compare('IsDeleted',$model->IsDeleted);
        $criteria->compare('AddedOn',$model->AddedOn,true);

        $criteria->order = "t.ID ASC";

        $getOrderDetails = Orders::model()->findAll($criteria);
        $row = 2;
        $grandTotal = 0.0;
        foreach($getOrderDetails as $details){
            $OrderID = $details->ID;
            $DealerName = $details->dealer->Name;
            $ExecutiveName = $details->submittedByUser->Name;
            $OrderStatus = $details->orderStatus->OrderStatus;
            $OrderDate = date('d-m-Y',strtotime($details->AddedOn));
            $OrderTotalAmount = $details->TotalAmount;
            $grandTotal = $grandTotal + $OrderTotalAmount;

            $columnValue=array($OrderID,$OrderDate,$DealerName,$ExecutiveName,$OrderStatus,$OrderTotalAmount);          


            for($col=0; $col<=ORDERREPORTCSVCOLUMNCOUNT; $col++){
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $columnValue[$col]); 
            }
            $row++;

        }
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(LABELCOUNT, $row, "Grand Total(Rs.)");
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(GRANDTOTALCOLUMNCOUNT, $row, $grandTotal);

        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle('Order Report');


        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // Redirect output to a client web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$fileName.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0


        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

        Yii::app()->end();
    }

当我尝试在实时服务器上打开 xls 文件时。它说 "The file you are trying to open , 'XXXX.xls' is in different format than specified by the file extension.Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?".

如果我说是,文件会打开,但它只包含特殊字符。

我确定它与 header 有关。但无法找出确切原因。请帮忙。

截图:

开始工作了:)只需在 header 之前清除输出缓冲区以确保没有任何内容被缓冲。

对于遇到相同问题的任何人,只需在上面代码的 header 之前添加这两行。

ob_end_clean();
ob_start();

它所做的首先是 erases/clear 输出缓冲并关闭输出缓冲。

然后我们再次打开输出缓冲。这样就不会从脚本发送任何输出(headers 除外),而是将输出存储在内部缓冲区中。