首頁  >  文章  >  後端開發  >  關於YII2框架中如何將excel表格匯出的圖文代碼教程

關於YII2框架中如何將excel表格匯出的圖文代碼教程

黄舟
黄舟原創
2017-07-22 15:01:54978瀏覽

最近在研究PHP的Yii框架,很喜歡,碰到導出Excel的問題,研究了一下,就有了下面這篇文章,這篇文章主要給大家介紹了關於YII2框架中excel表格導出的相關資料,文中透過範例程式碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

表格的匯入匯出是我們在日常開發中經常會遇到的功能,正巧在最近的專案中做到了關於表格輸出的功能,並且之前用TP的時候也做過,所以想著趁著這次功能比較多樣的機會整理一下,方便以後需要的時候,或者有需要的朋友們參考學習,下面話不多說了,來一起看看詳細的介紹:

本文是基於YII2框架進行開發的,不同框架可能會需要更改

一.普通excel格式表格輸出

先是最普通的匯出.xls格式的表格。首先先看一下表格在網站的顯示效果

這裡可以看到整個表格一共是7列。下面來看程式碼的實作。

1.controller檔案


#
//导出统计

public function actionStatistics(){
 //设置内存
 ini_set("memory_limit", "2048M");
 set_time_limit(0);

 //获取用户ID
 $id = Yii::$app->user->identity->getId();

 //去用户表获取用户信息
 $user = Employee::find()->where(['id'=>$id])->one();

 //获取传过来的信息(时间,公司ID之类的,根据需要查询资料生成表格)
 $params = Yii::$app->request->get();
 $objectPHPExcel = new \PHPExcel();

 //设置表格头的输出
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('A1', '代理公司');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('B1', '收入');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('C1', '成本');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('D1', '稿件数');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('E1', '毛利(收入-成本)');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('F1', '毛利率(毛利/收入)*100%');
 $objectPHPExcel->setActiveSheetIndex()->setCellValue('G1', 'ARPU值');

 //跳转到recharge这个model文件的statistics方法去处理数据
 $data = Recharge::statistics($params);

 //指定开始输出数据的行数
 $n = 2;
 foreach ($data as $v){
 $objectPHPExcel->getActiveSheet()->setCellValue('A'.($n) ,$v['company_name']);
 $objectPHPExcel->getActiveSheet()->setCellValue('B'.($n) ,$v['company_cost']);
 $objectPHPExcel->getActiveSheet()->setCellValue('C'.($n) ,$v['cost']);
 $objectPHPExcel->getActiveSheet()->setCellValue('D'.($n) ,$v['num']);
 $objectPHPExcel->getActiveSheet()->setCellValue('E'.($n) ,$v['gross_margin']);
 $objectPHPExcel->getActiveSheet()->setCellValue('F'.($n) ,$v['gross_profit_rate']);
 $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n) ,$v['arpu']);
 $n = $n +1;
 }
 ob_end_clean();
 ob_start();
 header('Content-Type : application/vnd.ms-excel');

 //设置输出文件名及格式
 header('Content-Disposition:attachment;filename="代理公司统计'.date("YmdHis").'.xls"');

 //导出.xls格式的话使用Excel5,若是想导出.xlsx需要使用Excel2007
 $objWriter= \PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
 $objWriter->save('php://output');
 ob_end_flush();

 //清空数据缓存
 unset($data);
}

2.model檔案


 <?php
 namespace app\models;//model层的命名空间
 //注意要引用yii的arrayhelper
 use yii\helpers\ArrayHelper;
 use Yii;
 class Recharge extends \yii\db\ActiveRecord
 {
 //excel一次导出条数
 const EXCEL_SIZE = 10000;
 
 //统计导出
 public static function statistics($params){

 //导出时间条件
 if(empty($params[&#39;min&#39;])){
 $date_max = date("Y-m-d",strtotime("-1 day"));
 $date_min = date("Y-m-d",strtotime("-31 day"));
 }else{
 $date_min = $params[&#39;min&#39;];
 $date_max = $params[&#39;max&#39;];
 }
 $where = &#39;&#39;;
 $where .= &#39;(`issue_date` BETWEEN &#39;.&#39;\&#39;&#39;.$date_min.&#39;\&#39;&#39;.&#39; AND &#39;.&#39;\&#39;&#39;.$date_max.&#39;\&#39;)&#39;;

 //查找指定数据
 $sql = &#39;select
 article.company_id,
 article.cost,
 article.company_cost
 from article WHERE article.status=2 AND &#39;.$where;
 $article = Article::findBySql($sql)->asArray()->all();
 $article = ArrayHelper::index($article,null,&#39;company_id&#39;);
 $companys = [];

 foreach ($article as $key=>$v){
 if(empty($key)){
 continue;
 }else{
 $number = count($v);
 $company = Company::find()->where([&#39;id&#39;=>$key])->select(&#39;name&#39;)->one();
 $company_name = $company[&#39;name&#39;];
 $cost = 0;
 $company_cost = 0;
 foreach ($v as $n){
 $cost += $n[&#39;cost&#39;];
 $company_cost += $n[&#39;company_cost&#39;];
 }
 if($company_cost == 0){
 $company_cost =1;
 }

 //这里注意,数据的存储顺序要和输出的表格里的顺序一样
 $companys[] = [
 //公司名
 &#39;company_name&#39; => $company_name,

 //收入
 &#39;company_cost&#39; => $company_cost,

 //成本
 &#39;cost&#39; => $cost,

 //稿件数
 &#39;num&#39; => $number,

 //毛利
 &#39;gross_margin&#39; => $company_cost-$cost,

 //毛利率
 &#39;gross_profit_rate&#39; => round(($company_cost-$cost)/$company_cost*100,2).&#39;%&#39;,

 //ARPU值
 &#39;arpu&#39; => round($company_cost/$number,2),
 ];
 }
 }
 return $companys;
 }
}

最終匯出的效果(儲存格大小匯出後調整過)可以看到和網頁顯示的基本一樣。

二.大數據表格匯出

這時老闆說了,我們不能只看總和的數據,最好是把詳細數據也匯出出來。既然老闆發話了,就去做吧。還是照第一種的方法去做,結果提示我php崩潰了,再試一次發現提示寫入位元組超出。開啟php的設定檔php.ini


memory_limit = 128M

發現預設記憶體已經給到128M,應該是足夠的了。於是我打開資料庫一看,嚯!

接近83萬個的資料進行查詢並匯出,可不是會出問題嘛!怎麼辦呢,於是我Google了一下,發現對於大數據(2萬條以上)的導出,最好是以.csv的形式。不要說廢話,直接上程式碼

1.controller檔案


#
//导出清单

public function actionInventory(){
 ini_set("memory_limit", "2048M");
 set_time_limit(0);
 $id = Yii::$app->user->identity->getId();
 $user = Employee::find()->where([&#39;id&#39;=>$id])->one();
 $params = Yii::$app->request->get();
 
 //类似的,跳转到recharge这个model文件里的inventory方法去处理数据
 $data = Recharge::inventory($params);
 
 //设置导出的文件名
 $fileName = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, &#39;代理商统计清单&#39;.date("Y-m-d"));
 
 //设置表头
 $headlist = array(&#39;代理商&#39;,&#39;文章ID&#39;,&#39;文章标题&#39;,&#39;媒体&#39;,&#39;统计时间范围&#39;,&#39;状态&#39;,&#39;创建时间&#39;,&#39;审核时间&#39;,&#39;发稿时间&#39;,&#39;退稿时间&#39;,&#39;财务状态&#39;,&#39;成本&#39;,&#39;销售额&#39;,&#39;是否是预收款媒体类型&#39;,&#39;订单类别&#39;);
 header(&#39;Content-Type: application/vnd.ms-excel&#39;);
 
 //指明导出的格式
 header(&#39;Content-Disposition: attachment;filename="&#39;.$fileName.&#39;.csv"&#39;);
 header(&#39;Cache-Control: max-age=0&#39;);
 
 //打开PHP文件句柄,php://output 表示直接输出到浏览器
 $fp = fopen(&#39;php://output&#39;, &#39;a&#39;);
 
 //输出Excel列名信息
 foreach ($headlist as $key => $value) {
 //CSV的Excel支持GBK编码,一定要转换,否则乱码
 $headlist[$key] = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $value);
 }
 
 //将数据通过fputcsv写到文件句柄
 fputcsv($fp, $headlist);
 
 //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
 $limit = 100000;
 
 //逐行取出数据,不浪费内存
 foreach ($data as $k => $v) {
 //刷新一下输出buffer,防止由于数据过多造成问题
 if ($k % $limit == 0 && $k!=0) {
 ob_flush();
 flush();
 }
 $row = $data[$k];
 foreach ($row as $key => $value) {
 $row[$key] = iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $value);
 }
 fputcsv($fp, $row);
 }
}

2.model檔案(因為這部分我要處理的太多,所以只選擇了部分程式碼),在查詢資料那部分,因為要查的資料較多,所以可以結合我之前寫的關於Mysql大數據查詢處理的文章看一下

//清單匯出


public static function inventory($params){
 //统计时间范围
 if(!empty($params[&#39;min&#39;]) && !empty($params[&#39;max&#39;])){
 $ti = strtotime($params[&#39;max&#39;])+3600*24;
 $max = date(&#39;Y-m-d&#39;,$ti);
 $time = $params[&#39;min&#39;].&#39;-&#39;.$params[&#39;max&#39;];
 $date_min = $params[&#39;min&#39;];
 $date_max = $max;
 }else{
 $date_max = date(&#39;Y-m-d&#39;);
 $date_min = date(&#39;Y-m-d&#39;,strtotime("-31 day"));
 $time = $date_min.&#39;-&#39;.$date_max;
 }
 //查询数据
 if($params[&#39;state&#39;] == 1){
 $where = &#39;&#39;;
 $where .= &#39; AND (`issue_date` BETWEEN &#39;.&#39;\&#39;&#39;.$date_min.&#39;\&#39;&#39;.&#39; AND &#39;.&#39;\&#39;&#39;.$date_max.&#39;\&#39;)&#39;;
 $map = &#39;select
  company.name,
  article.id,
  article.title,
  media.media_name,
  article.status,
  article.created,
  article.audit_at,
  article.issue_date,
  article.back_date,
  article.finance_status,
  article.cost,
  article.company_cost,
  media.is_advance
  from article
  LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
  LEFT JOIN `order` ON custom_package.order_id = `order`.`id`
  LEFT JOIN company ON company.id = article.company_id
  LEFT JOIN media ON media.id = article.media_id
  where article.status=2 and `order`.package=0&#39;.$where;
 //查找的第一部分数据,使用asArray方法可以使我们查找的结果直接形成数组的形式,没有其他多余的数据占空间(注意:我这里查找分三部分是因为我要查三种不同的数据)
 $list1 = Article::findBySql($map)->asArray()->all();
 $where2 = &#39;&#39;;
 $where2 .= &#39; AND (`issue_date` BETWEEN &#39;.&#39;\&#39;&#39;.$date_min.&#39;\&#39;&#39;.&#39; AND &#39;.&#39;\&#39;&#39;.$date_max.&#39;\&#39;)&#39;;
 $where2 .= &#39; AND (`back_date` > \&#39;&#39;.$date_max.&#39;\&#39;)&#39;;
 $map2 = &#39;select
  company.name,
  article.id,
  article.title,
  media.media_name,
  article.status,
  article.created,
  article.audit_at,
  article.issue_date,
  article.back_date,
  article.finance_status,
  article.cost,
  article.company_cost,
  media.is_advance
  from article
  LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
  LEFT JOIN `order` ON custom_package.order_id = `order`.`id`
  LEFT JOIN company ON company.id = article.company_id
  LEFT JOIN media ON media.id = article.media_id
  where article.status=3 and `order`.package=0 &#39;.$where2;
 //查找的第二部分数据
 $list2 = Article::findBySql($map2)->asArray()->all();
 $where3 = &#39;&#39;;
 $where3 .= &#39; AND (`issue_date` BETWEEN &#39;.&#39;\&#39;&#39;.$date_min.&#39;\&#39;&#39;.&#39; AND &#39;.&#39;\&#39;&#39;.$date_max.&#39;\&#39;)&#39;;
 $map3 = &#39;select
  company.name,
  article.id,
  article.title,
  media.media_name,
  article.status,
  article.created,
  article.audit_at,
  article.issue_date,
  article.back_date,
  article.finance_status,
  article.cost,
  article.company_cost,
  media.is_advance
  from article
  LEFT JOIN custom_package ON custom_package.id = article.custom_package_id
  LEFT JOIN `order` ON custom_package.order_id = `order`.`id`
  LEFT JOIN company ON company.id = article.company_id
  LEFT JOIN media ON media.id = article.media_id
  where article.status=5 &#39;.$where3;
 //查找的第三部分数据
 $list3 = Article::findBySql($map3)->asArray()->all();
 $list4 = ArrayHelper::merge($list1,$list2);
 $list = ArrayHelper::merge($list4,$list3);
 }
 //把结果按照显示顺序存到返回的数组中
 if(!empty($list)){
 foreach ($list as $key => $value){
 //代理公司
 $inventory[$key][&#39;company_name&#39;] = $value[&#39;name&#39;];
 //文章ID
 $inventory[$key][&#39;id&#39;] = $value[&#39;id&#39;];
 //文章标题
 $inventory[$key][&#39;title&#39;] = $value[&#39;title&#39;];
 //媒体
 $inventory[$key][&#39;media&#39;] = $value[&#39;media_name&#39;];
 //统计时间
 $inventory[$key][&#39;time&#39;] = $time;
 //状态
 switch($value[&#39;status&#39;]){
 case 2:
  $inventory[$key][&#39;status&#39;] = &#39;已发布&#39;;
  break;
 case 3:
  $inventory[$key][&#39;status&#39;] = &#39;已退稿&#39;;
  break;
 case 5:
  $inventory[$key][&#39;status&#39;] = &#39;异常稿件&#39;;
  break;
 }
 //创建时间
 $inventory[$key][&#39;created&#39;] = $value[&#39;created&#39;];
 //审核时间
 $inventory[$key][&#39;audit&#39;] = $value[&#39;audit_at&#39;];
 //发稿时间
 $inventory[$key][&#39;issue_date&#39;] = $value[&#39;issue_date&#39;];
 //退稿时间
 $inventory[$key][&#39;back_date&#39;] = $value[&#39;back_date&#39;];
 //财务状态
 switch($value[&#39;finance_status&#39;]){
 case 0:
  $inventory[$key][&#39;finance_status&#39;] = &#39;未到结算期&#39;;
  break;
 case 1:
  $inventory[$key][&#39;finance_status&#39;] = &#39;可结算&#39;;
  break;
 case 2:
  $inventory[$key][&#39;finance_status&#39;] = &#39;资源审批中&#39;;
  break;
 case 3:
  $inventory[$key][&#39;finance_status&#39;] = &#39;财务审批中&#39;;
  break;
 case 4:
  $inventory[$key][&#39;finance_status&#39;] = &#39;已结款&#39;;
  break;
 case 5:
  $inventory[$key][&#39;finance_status&#39;] = &#39;未通过&#39;;
  break;
 case 6:
  $inventory[$key][&#39;finance_status&#39;] = &#39;财务已审批&#39;;
  break;
 }
 //成本
 $inventory[$key][&#39;cost&#39;] = $value[&#39;cost&#39;];
 //销售额
 $inventory[$key][&#39;company_cost&#39;] = $value[&#39;company_cost&#39;];
 //是否是预售
 switch($value[&#39;is_advance&#39;]){
 case 0:
  $inventory[$key][&#39;is_advance&#39;] = &#39;否&#39;;
  break;
 case 1:
  $inventory[$key][&#39;is_advance&#39;] = &#39;是&#39;;
  break;
 case 2:
  $inventory[$key][&#39;is_advance&#39;] = &#39;合同&#39;;
  break;
 }
 //订单类别
 switch($params[&#39;state&#39;]){
 case 1:
  $inventory[$key][&#39;order_type&#39;] = &#39;时间区间无退稿完成订单&#39;;
  break;
 case 2:
  $inventory[$key][&#39;order_type&#39;] = &#39;时间区间发布前退稿订单&#39;;
  break;
 case 3:
  $inventory[$key][&#39;order_type&#39;] = &#39;时间区间发布后时间区间退稿订单&#39;;
  break;
 case 4:
  $inventory[$key][&#39;order_type&#39;] = &#39;时间区间之前发布时间区间内退稿订单&#39;;
  break;
 case 5:
  $inventory[$key][&#39;order_type&#39;] = &#39;异常订单&#39;;
  break;
 }
 }
 }else{
 $inventory[0][&#39;company_name&#39;] = &#39;无数据导出&#39;;
 }
 return $inventory;
}

3.匯出結果

匯出數量

匯出的檔案

#基本上可以保證整個過程在2~4秒內處理完成

三.合併儲存格

老闆一看做的不錯,說你順便把儲值統計的匯出也做了把,想想我都是處理過這麼多數據的人了,還不是分分鐘搞定的事?來,上原型圖

噗,一口老血,話都說了,搞吧。在做的時候我發現,這次的匯出主要是要解決儲存格合併的問題。經過查資料發現,PHP本身是實現不了單元格合併的,所以我打算透過phpexcel來實現

#如果是使用PHPExcel的話,基本操作是這樣的(合併A1到E1)


$objPHPExcel->getActiveSheet()->mergeCells(&#39;A1:E1&#39;);
// 表格填充内容
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A1&#39;,&#39;The quick brown fox.&#39;);

結果

或這樣的(合併A1到E4)


$objPHPExcel->getActiveSheet()->mergeCells(&#39;A1:E4&#39;);
$objPHPExcel->getActiveSheet()->setCellValue(&#39;A1&#39;,&#39;The quick brown fox.&#39;);

結果

這樣並不能滿足我的要求,首先它是一個一個合併的,其次我要顯示的充值金額下面的類型是會變化的,不可能固定寫死,然後每次都更改。所以放棄了這種方法。

後來在小夥伴的幫助下嘗試用html轉存excel的方法

1.方法檔案(因為我要每天定時執行,所以並沒有寫到controller層)


#
public function actionExcelRechargeStatistics(){

 //先定义一个excel文件
 $filename = date(&#39;【充值统计表】(&#39;.date(&#39;Y-m-d&#39;).&#39;导出)&#39;).".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[&#39;min&#39;])){
 $time = date(&#39;Y-m-d&#39;,strtotime("+1 day"));
 $where = &#39; created < \&#39; &#39;.$time.&#39;\&#39;&#39;;
 }else{
 $time = $params[&#39;min&#39;]+3600*24;
 $time_end = $params[&#39;max&#39;]+3600*24;
 $where = &#39; created <= \&#39; &#39;.$time_end.&#39;\&#39; AND created >= \&#39;&#39;.$time.&#39;\&#39; &#39;;
 }
 //充值类型列表
 $recharge_type = Recharge::find()->asArray()->all();
 if(empty($recharge_type)){
 $rechargelist[0]= &#39;&#39;;
 }else{
 $rechargelist = ArrayHelper::map($recharge_type,&#39;id&#39;,&#39;recharge_name&#39;);
 }
 $rechargelist1 = $rechargelist;
 $count = count($rechargelist1);
 //使用html语句生成显示的格式
 $excel_content = &#39;<meta http-equiv="content-type" content="application/ms-excel; charset=utf-8"/>&#39;;
 $excel_content .= &#39;<table border="1" style="font-size:14px;">&#39;;
 $excel_content .= &#39;<thead>
   <tr>
   <th rowspan="2">ID</th>
   <th rowspan="2">公司名称</th>
   <th colspan=&#39;.$count.&#39;>充值金额</th>
   <th rowspan="2">充值大小</th>
   <th rowspan="2">实际消费</th>
   <th rowspan="2">当前余额</th>
   </tr>
   <tr>
  &#39;;
 foreach ($rechargelist1 as $v => $t){
 $excel_content .= &#39;<th colspan="1">&#39;.$t.&#39;</th>&#39;;
 }
 $excel_content .= &#39;</tr>
  </thead>&#39;;
 //查找最新的固化数据
 $search = RechargeStatistics::find()->where($where)->asArray()->all();
 if(!empty($search)){
 foreach ($search as $key => $value){
 $search[$key][&#39;recharge&#39;] = unserialize($value[&#39;recharge&#39;]);
 }
 }
 //html语句填充数据
 if(empty($search)){
 }else{
 foreach ($search as $k) {
 $excel_content .= &#39;<td>&#39;.$k[&#39;company_id&#39;].&#39;</td>&#39;;
 $excel_content .= &#39;<td>&#39;.$k[&#39;company_name&#39;].&#39;</td>&#39;;
 foreach ($rechargelist1 as $v=>$t){
 $price = 0;
 foreach ($k[&#39;recharge&#39;] as $q=>$w){
  if($w[&#39;recharge_id&#39;] == $v){
  $price = $w[&#39;price&#39;];
  break;
  }
 }
 $excel_content .= &#39;<td>&#39;.$price.&#39;</td>&#39;;
 }
 $excel_content .= &#39;<td>&#39;.$k[&#39;total&#39;].&#39;</td>&#39;;
 $excel_content .= &#39;<td>&#39;.$k[&#39;consume&#39;].&#39;</td>&#39;;
 $excel_content .= &#39;<td>&#39;.($k[&#39;total&#39;]-$k[&#39;consume&#39;]).&#39;</td></tr>&#39;;
 }
 }
 $excel_content .= &#39;</table>&#39;;
 echo $excel_content;
 die;
}

2.结果

到这里基本就完成所有的任务了!

总结

以上是關於YII2框架中如何將excel表格匯出的圖文代碼教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn