Blogger Information
Blog 26
fans 1
comment 1
visits 35433
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据导出csv 和下载pdf
Bystander
Original
946 people have browsed it

第一步,组装导出列表

/**
     * 导出列表.
     */
    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(); 

 

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post