第一步,组装导出列表
/**
* 导出列表.
*/
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();