如果是使用PHPExcel的话,基本操作是这样的(合并A1到E1)
$objPHPExcel->getActiveSheet()->mergeCells('A1:E1'); // 表格填充内容 $objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
结果
或者这样的(合并A1到E4)
$objPHPExcel->getActiveSheet()->mergeCells('A1:E4'); $objPHPExcel->getActiveSheet()->setCellValue('A1','The quick brown fox.');
结果
这样并不能满足我的要求,首先它是一个一个合并的,其次我要显示的充值金额下面的类型是会变化的,不可能固定写死,然后每次都更改。所以放弃了这种方法。
后来在小伙伴的帮助下尝试用html转存excel的方法
1.方法文件(因为我要每天定时执行,所以并没有写到controller层)
public function actionExcelRechargeStatistics(){ //先定义一个excel文件 $filename = date('【充值统计表】('.date('Y-m-d').'导出)').".xls"; header("Content-Type: application/vnd.ms-execl"); header("Content-Type: application/vnd.ms-excel; charset=utf-8"); header("Content-Disposition: attachment; filename=$filename"); header("Pragma: no-cache"); header("Expires: 0"); //时间条件 if(empty($params['min'])){ $time = date('Y-m-d',strtotime("+1 day")); $where = ' created < \' '.$time.'\''; }else{ $time = $params['min']+3600*24; $time_end = $params['max']+3600*24; $where = ' created <= \' '.$time_end.'\' AND created >= \''.$time.'\' '; } //充值类型列表 $recharge_type = Recharge::find()->asArray()->all(); if(empty($recharge_type)){ $rechargelist[0]= ''; }else{ $rechargelist = ArrayHelper::map($recharge_type,'id','recharge_name'); } $rechargelist1 = $rechargelist; $count = count($rechargelist1); //使用html语句生成显示的格式 $excel_content = '<meta http-equiv="content-type" content="application/ms-excel; charset=utf-8"/>'; $excel_content .= '<table border="1" style="font-size:14px;">'; $excel_content .= '<thead> <tr> <th rowspan="2">ID</th> <th rowspan="2">公司名称</th> <th colspan='.$count.'>充值金额</th> <th rowspan="2">充值大小</th> <th rowspan="2">实际消费</th> <th rowspan="2">当前余额</th> </tr> <tr> '; foreach ($rechargelist1 as $v => $t){ $excel_content .= '<th colspan="1">'.$t.'</th>'; } $excel_content .= '</tr> </thead>'; //查找最新的固化数据 $search = RechargeStatistics::find()->where($where)->asArray()->all(); if(!empty($search)){ foreach ($search as $key => $value){ $search[$key]['recharge'] = unserialize($value['recharge']); } } //html语句填充数据 if(empty($search)){ }else{ foreach ($search as $k) { $excel_content .= '<td>'.$k['company_id'].'</td>'; $excel_content .= '<td>'.$k['company_name'].'</td>'; foreach ($rechargelist1 as $v=>$t){ $price = 0; foreach ($k['recharge'] as $q=>$w){ if($w['recharge_id'] == $v){ $price = $w['price']; break; } } $excel_content .= '<td>'.$price.'</td>'; } $excel_content .= '<td>'.$k['total'].'</td>'; $excel_content .= '<td>'.$k['consume'].'</td>'; $excel_content .= '<td>'.($k['total']-$k['consume']).'</td></tr>'; } } $excel_content .= '</table>'; echo $excel_content; die; }
2.结果
到这里基本就完成所有的任务了!
总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。