博客列表 >数据导出csv 和下载pdf

数据导出csv 和下载pdf

Bystander
Bystander原创
2018年12月10日 17:37:54945浏览

第一步,组装导出列表

/**
     * 导出列表.
     */
    public function actionExport()
    {
         

$title = '序号,活动名称,用户姓名,手机号'."\n";
        $fileName = '参加用户信息'.date('Ymd').'.csv';

        //  列表
        $dataArr = Ticket::find()->alias('t')->leftJoin(['tu' => TicketUser::tableName()], 'tu.ticket_id=t.id')
            ->where(['t.is_delete' => 0,'t.id'=>$id])->select('t.title,tu.id,tu.user_name,tu.mobile')->asArray()->all();

        $wrstr = '';

        foreach ($dataArr as $k => $v) {
            if(!$v['user_name']){
                $dataArr = [];
            }
        }
        
        $sort = 0;
        if(!empty($dataArr)){
            foreach ($dataArr as $k => $v) {
                $sort = $sort + 1;
                $wrstr .= $sort . ',' .$v['title'] . ',' . $v['user_name'] . ',' . $v['mobile'];
                $wrstr .= "\n";
            }
        }

        $this->Csvexport( $fileName, $title, $wrstr);


          
    }
---------------------
 第二步,导出数据


    public function Csvexport($file = '', $title = '', $data)
    {
        header("Content-Disposition:attachment;filename=".$file);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');

        // ob_start();

        //表头
        $wrstr = $title;

        //数据
        $wrstr .= $data;

        $wrstr = iconv("utf-8", "GBK//ignore", $wrstr);

        // ob_end_clean();

        echo $wrstr;

    }
 


2.下载页面 PDF 可使用浏览器自带功能打印,自动调用方法 window.print(); 

 

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议