先composer回来phpexcel的包,
use PHPExcel;use PHPExcel_Writer_Excel2007;use PHPExcel_Style_Border;use PHPExcel_Style_Alignment;use PHPExcel_IOFactory;
public function actionTixianExport($id){ $model = $this->findModel($id); $orders = \common\models\Order::find()->where(['in','id',explode(",", $model->orderId)])->all(); error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); $objPHPExcel = new PHPExcel(); /* 以下是一些设置 ,什么作者 标题啊之类的 */ $objPHPExcel->getProperties()->setCreator("河南亿飞网络科技有限公司") ->setLastModifiedBy("刘明阳") ->setTitle("数据EXCEL导出") ->setSubject("数据EXCEL导出") ->setDescription("提现申请表") ->setKeywords("excel") ->setCategory("result file"); $i = 1; $objPHPExcel->getActiveSheet()->mergeCells('A1:E1'); $objPHPExcel->getActiveSheet()->setTitle('提现申请表'); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i, '提现申请表'); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$i)->applyFromArray( array( 'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER ) ) ); $i = 2; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i, '申请日期:'.date("Y-m-d H:i:s",$model->created_at)) ->setCellValue('B'.$i, '商家名称:'.$model->company_name) ->setCellValue('C'.$i, '类型:'.ProviderTixian::tixianlabels($model->type)) ->setCellValue('D'.$i, '姓名:'.$model->xingming) ->setCellValue('E'.$i, '账号:'.$model->kaihuhang ." ". $model->haoma); $i = 3; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i, '订单日期') ->setCellValue('B'.$i, '订单号') ->setCellValue('C'.$i, '接单时间') ->setCellValue('D'.$i, '完成时间') ->setCellValue('E'.$i, '金额'); $i = 4; //查询数据 foreach ($orders as $v) { $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,date("Y-m-d H:i:s",$v['created_at'])) ->setCellValue('B'.$i,substr($v['sn'],4)) ->setCellValue('C'.$i,date("Y-m-d H:i:s",$v['qiangdan_shijian'])) ->setCellValue('D'.$i,date("Y-m-d H:i:s",$v['wancheng_shijian'])) ->setCellValue('E'.$i,$v['total_price']); $i++; } $objPHPExcel->getActiveSheet()->mergeCells('A'.$i.':E'.$i); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,"总额:".$model->jine); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$i)->applyFromArray( array( 'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER ) ) ); $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="提现申请表.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; }
这种是靠excel的兼容性做的,不依赖任何第三方扩展 ,存在一定的兼容问题
public function actionTixianExportBak($id){ //申明头部,生成excel类型文件 header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=提现申请表.xls"); $model = $this->findModel($id); $orders = \common\models\Order::find()->where(['in','id',explode(",", $model->orderId)])->all(); //获取所有表字段 $type = array('2' => '普通企业用户', '3' => '高级企业用户', '4' => 'VIP企业用户', '1' => '个人用户',); //制作表头 echo "
提现申请单 | ||||
---|---|---|---|---|
申请日期:" . date("Y-m-d H:i:s",$model->created_at) . " | "; echo "商家名称:" . $model->company_name . " | "; echo "类型:" . ProviderTixian::tixianlabels($model->type) . " | "; echo "姓名:" . $model->xingming . " | "; echo "账号:" . $model->kaihuhang ." ". $model->haoma. " | "; echo "
订单日期 | "; echo "订单号 | "; echo "接单时间 | "; echo "完成时间 | "; echo "金额 | "; echo "
" . date("Y-m-d H:i:s",$v[$column]) . " | "; }else if($k == 1){ echo "" . substr($v[$column], 3) . " | "; }else{ echo "" . $v[$column] . " | "; } } echo "||
总额:".$model->jine." |