数据导出在项目开发中非常常用。不过大家常用的就是PHPExcel类库了。只可惜这个类库有时候composer安装老装不上。所以波波专门写一篇博客来分享这个类库以及该类库的使用方法。
操作步骤:
1、安装类库。如果你的composer能用,那么建议用composer方式安装类库,如果不能用就点击下方链接下载即可。
类库下载地址:
链接:https://pan.baidu.com/s/15r5QDtP9s-lv8TIQWReTlw 密码:itwl
2、使用时控制器参考示例:
- /*
- * 提现数据导出
- */
- public function export_tixian(){
- $list = Db::name("SystemMoneyApply")->where(['id'=>['>','0']])
- ->field('member_id,amount,fee,heart_fund,tax,huitian,realpay,addtime,operator,status')->select();
- Vendor("PHPExcel.PHPExcel");//引入phpexcel类(注意你自己的路径)
- $objPHPExcel = new \PHPExcel();
- $title='提现数据';
- $xlsTitle = iconv('utf-8', 'gb2312', $title);//文件名称
- $fileName = $title.date('_YmdHis');//文件名称
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);//设置列宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);//设置列宽度
- //处理表头
- $objPHPExcel->getActiveSheet()->setCellValue("A1","用户ID")
- ->setCellValue("B1","会员账号")->setCellValue("C1","申请金额")
- ->setCellValue("D1","手续费")->setCellValue("E1","爱心基金")
- ->setCellValue("F1","税费")->setCellValue("G1","回填费")
- ->setCellValue("H1","应付金额")->setCellValue("I1","申请时间")
- ->setCellValue("J1","处理人")->setCellValue("K1","状态");
- //处理数据
- $j = 2;//行数
- foreach($list as $key => $val){
- $arr = ["待处理","已处理","已拒绝"];
- $status = $arr[$val['status']];
- $objPHPExcel->getActiveSheet()->setCellValue("A".$j,$val['member_id'])->setCellValue("B".$j,getUserField($val['member_id'],'account'))
- ->setCellValue("C".$j,$val['amount'])->setCellValue("D".$j,$val['fee'])
- ->setCellValue("E".$j,$val['heart_fund'])->setCellValue("F".$j,$val['tax'])
- ->setCellValue("G".$j,$val['huitian'])->setCellValue("H".$j,$val['realpay'])
- ->setCellValue("I".$j,$val['addtime'])->setCellValue("J".$j,getAdminName($val['operator']))
- ->setCellValue("K".$j,$status);
- $j++;
- unset($status);
- }
- //导出execl
- header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
- header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- $objWriter->save("php://output");
- }
3、在上述示例中波波仅将phpexcel类库文件夹复制到了vendor目录下。已测试OK!
如果朋友们在使用过程中有任何疑问,也可以留言给我。