我正在尝试将gridview导出到Yii中的excel xls。为此,我使用了PHPExcel扩展。它在localhost上工作,但不在实时服务器上工作。
以下是我的模特功能:
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文件时。它说&#34;您要打开的文件,&#39; XXXX.xls&#39;格式与文件扩展名指定的格式不同。在打开文件之前,验证文件是否已损坏且来自受信任的源。你想现在打开文件吗?&#34;。
如果我说是,文件被打开但它只包含特殊字符。
我确信它与标题有关。但无法找出确切的原因。请帮忙。
截图:
答案 0 :(得分:1)
搞定了工作:)只需在标题之前清除输出缓冲区,以确保没有任何缓冲。
对于遇到相同问题的任何人,只需在上面代码中的标题之前添加这两行。
ob_end_clean();
ob_start();
它的作用是首先擦除/清除输出缓冲区并关闭输出缓冲。
然后我们再次打开输出缓冲。因此,不会从脚本(标题除外)发送输出,而是将输出存储在内部缓冲区中。