博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
yii2 phpexcel导出excel
阅读量:5026 次
发布时间:2019-06-12

本文共 5721 字,大约阅读时间需要 19 分钟。

先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 "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; echo "
"; $columns = ['created_at','sn','qiangdan_shijian','wancheng_shijian','total_price']; //查询数据 foreach ($orders as $v) { echo "
"; foreach($columns as $k=>$column){ if($k == 0 || $k == 2 || $k == 3){ echo "
"; }else if($k == 1){ echo "
"; }else{ echo "
"; } } echo "
"; } echo "
"; echo "
提现申请单
申请日期:" . date("Y-m-d H:i:s",$model->created_at) . " 商家名称:" . $model->company_name . " 类型:" . ProviderTixian::tixianlabels($model->type) . " 姓名:" . $model->xingming . " 账号:" . $model->kaihuhang ."  ". $model->haoma. "
订单日期 订单号 接单时间 完成时间 金额
" . date("Y-m-d H:i:s",$v[$column]) . " " . substr($v[$column], 3) . " " . $v[$column] . "
总额:".$model->jine."
"; }

  

转载于:https://www.cnblogs.com/james888/p/5811018.html

你可能感兴趣的文章
软件工程APP进度更新
查看>>
Python 使用正则替换 re.sub
查看>>
CTF中那些脑洞大开的编码和加密
查看>>
简化工作流程 10款必备的HTML5开发工具
查看>>
c++ 调用外部程序exe-ShellExecuteEx
查看>>
Java进击C#——语法之知识点的改进
查看>>
IdentityServer流程图与相关术语
查看>>
BirdNet: a 3D Object Detection Framework from LiDAR information
查看>>
icon fonts入门
查看>>
【Django】如何按天 小时等查询统计?
查看>>
HDU2191(多重背包)
查看>>
测试用例(一)
查看>>
【转】 mysql反引号的使用(防冲突)
查看>>
邮件中的样式问题
查看>>
AJAX 状态值与状态码详解
查看>>
php面向对象编程(oop)基础知识示例解释
查看>>
1.在数组中找到与给定总和的配对
查看>>
树的子结构
查看>>
关于根据Build Platform或者OS 加载x86或者x64 dll的问题
查看>>
程序员高效开发的几个技巧
查看>>