PHP 下载mysql数据到PHPExcel表格

第一步:先到官网(https://github.com/PHPOffice/PHPExcel)下载PHPExcel

 

第二步:放到第三方库

 

第三步:

 /**
     * 封装:信息导出
     * @param $day
     * @param string $tableName
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     */
    public function Db2Eexcel($data, $tableName = "用户支付数据"){

        vendor("PHPExcel.PHPExcel");

        // 1. 实例化工作簿
        $excel = new \PHPExcel();

        // 2. 得到前当活动工作表
        $sheet = $excel->getActiveSheet();

        // 3. Excel表格式,这里简略写了14列
        $letter = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'];

        // 3.1 表头数组
        $tableheader = ['ID','订单创建时间','订单处理时间','支付平台单号','商户订单号','支付类型','支付金额','实际支付金额','银行卡','商品名称','支付状态','游戏支付状态','账号','服务器ID','渠道','子渠道','游戏ID'];

        // 3.2 填充表头信息, 并设置样式
        for($i = 0;$i < count($tableheader);$i++) {
            $sheet->getCell("$letter[$i]1")->setValue("$tableheader[$i]");
            $sheet->getStyle("$letter[$i]1")->getAlignment()->setHorizontal('center');    // 统一设置水平居中
            $sheet->getStyle("$letter[$i]1")->getAlignment()->setVertical('center');      // 统一设置垂直居中
            $sheet->getStyle("$letter[$i]1")->getFont()->getColor()->setRGB('b5211a');    // 统一设置单元格字段颜色
            $sheet->getStyle("$letter[$i]1")->getFont()->setSize(10);                     // 统一设置字体大小
            $sheet->getStyle("$letter[$i]")->getFont()->setSize(10);
            $sheet->getStyle("$letter[$i]1")->getFont()->setBold(true);                   // 统一设置加粗
            $sheet->getColumnDimension("$letter[$i]")->setWidth(20);                      // 统一设置宽度
        }

        // 3.3 样式:第1行的高度
        $sheet->getRowDimension('1')->setRowHeight(30);

        // 3.4 样式:列的宽度
        $sheet->getColumnDimension('A')->setWidth(10);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(20);
        $sheet->getColumnDimension('D')->setWidth(30);
        $sheet->getColumnDimension('E')->setWidth(30);
        $sheet->getColumnDimension('F')->setWidth(10);
        $sheet->getColumnDimension('G')->setWidth(10);
        $sheet->getColumnDimension('H')->setWidth(15);
        $sheet->getColumnDimension('I')->setWidth(10);
        $sheet->getColumnDimension('J')->setWidth(10);
        $sheet->getColumnDimension('K')->setWidth(10);
        $sheet->getColumnDimension('L')->setWidth(15);
        $sheet->getColumnDimension('M')->setWidth(10);
        $sheet->getColumnDimension('N')->setWidth(10);
        $sheet->getColumnDimension('O')->setWidth(10);
        $sheet->getColumnDimension('P')->setWidth(10);
        $sheet->getColumnDimension('Q')->setWidth(20);

        // 4. 填充表格信息
        for ($i = 2, $k = 0; $i <= count($data)+1; $i++, $k++) {
            $j = 0;

            // 格式化:时间
            if(!empty($data[$k]['ftime'])){
                $data[$k]['ftime'] = date('Y/m/d H:i:s', $data[$k]['ftime']);
            }
            if(!empty($data[$k]['ptime'])){
                $data[$k]['ptime'] = date('Y/m/d H:i:s', $data[$k]['ptime']);
            }

            // 格式化:支付类型 1:支付宝 2:微信
            if(!empty($data[$k]['paytype'])){
                if($data[$k]['paytype'] == 1){
                    $data[$k]['paytype'] = "支付宝";
                }
                if($data[$k]['paytype'] == 2){
                    $data[$k]['paytype'] = "微信";
                }
            }

            //格式化:支付状态1成功0失败
            if(isset($data[$k]['paystatus']))
            {
                $data[$k]['paystatus'] = $data[$k]['paystatus'] == 1 ? "成功" : "失败";
            }
            if(isset($data[$k]['gamepaystatus']))
            {
                $data[$k]['gamepaystatus'] = $data[$k]['gamepaystatus'] == 1 ? "成功" : "失败";
            }

            foreach ($data[$i - 2] as $key=>$value) {
                $sheet->getCell("$letter[$j]$i")->setValue("$value");
                $sheet->getStyle("$letter[$j]$i")->getAlignment()->setHorizontal('center');
                $j++;
            }
        }

        // 6. 保存文件:输出到浏览器
        $writer = \PHPExcel_IOFactory::createWriter($excel,'Excel2007');
        self::browser_export('Excel2007', $tableName.'.xlsx');    //输出到浏览器
        $writer->save("php://output");
    }

    /**
     * 输出到浏览器
     * @param $type
     * @param $filename
     */
    function browser_export($type,$filename){
        if($type=="Excel5"){
            header('Content-Type: application/vnd.ms-excel');//告诉浏览器将要输出excel03文件
        }else{
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;');//告诉浏览器数据excel07文件
        }
        ob_end_clean();//清除缓存区,避免乱码
        header('Content-Disposition: attachment;filename="'.$filename.'"'); //告诉浏览器将输出文件的名称
        header('Cache-Control: max-age=0');                                 //禁止缓存
    }

 

转载于:https://www.cnblogs.com/cqlb/p/9593837.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHPWEB产品模块增加下载文件(下载里面自带修改好的文件): 1:product\admin\product_conadd.php 页面, 这是产品新增页面 从134行起新增如下代码, 是用来上传文件的: <!--新增上传文件 开始 --> <tr> <td height="30" width="100" align="center">文件上传</td> <td height="30" valign="top"> <input id="divsuo" type="file" name="file" style="width: 499px; display: none;" class="input"> <input id="divurl" type="text" name="fileurl" style="width: 499px; display: inline;" value="http://" class="input"> <input type="radio" name="addtype" value="addurl" checked="" onclick="document.getElementById('divurl').style.display='inline';document.getElementById('divsuo').style.display='none';"> 链接网址 <input type="radio" name="addtype" value="addfile" onclick="document.getElementById('divurl').style.display='none';document.getElementById('divsuo').style.display='inline';"> 上传文件 </td> </tr> <!--新增上传文件 结束 --> 2:product\admin\product_conmod.php页面, 这是产品的修改页面 从188行起新增如下代码, 是用来上传文件的: <!--新增上传文件 开始 --> <tr> <td height="30" width="100" align="center">文件上传</td> <td height="30" valign="top"> <input id="divsuo" type="file" name="file" style="width: 499px; display: none;" class="input"> <input id="divurl" type="text" name="fileurl" style="width: 499px; display: inline;" value="<?php echo $fileurl; ?>" class="input"> <input type="radio" name="addtype" value="addurl" checked="" onclick="document.getElementById('divurl').style.display='inline';document.getElementById('divsuo').style.display='none';"> 链接网址 <input type="radio" name="addtype" value="addfile" onclick="document.getElementById('divurl').style.display='none';document.getElementById('divsuo').style.display='inline';"> 上传文件 </td> </tr> <!--新增上传文件 结束 --> 3:product\admin\post.php页面, 这是产品的获取数据用的 281行代码注释, 新增282行代码: //新增下载 $msql->query( "update {P}_product_con set \r\n\t\t\ttitle='{$title}',\r\n\t\t\tmemo='{$memo}',\r\n\t\t body='{$body}',\r\n\t\t\tcatid='{$catid}',\r\n\t\t\tcatpath='{$catpath}',\r\n\t\t\tuptime='{$uptime}',\r\n\t\t\tauthor='{$author}',\r\n\t\t\tsource='{$source}',\r\n\t\t\tproj='{$projpath}',\r\n\t\t\ttags='{$tagstr}',\r\n\t\t\tprop1='{$prop1}',\r\n\t\t\tprop2='{$prop2}',\r\n\t\t\tprop3='{$prop3}',\r\n\t\t\tprop4='{$prop4}',\r\n\t\t\tprop5='{$prop5}',\r\n\t\t\tprop6='{$prop6}',\r\n\t\t\tprop7='{$prop7}',\r\n\t\t\tprop8='{$prop8}',\r\n\t\t\tprop9='{$prop9}',\r\n\t\t\tprop10='{$prop10}',\r\n\t\t\tprop11='{$prop11}',\r\n\t\t\tprop12='{$prop12}',\r\n\t\t\tprop13='{$prop13}',\r\n\t\t\tprop14='{$prop14}',\r\n\t\t\tprop15='{$prop15}',\r\n\t\t\tprop16='{$prop16}',\r\n\t\t\tprop17='{$prop17}',\r\n\t\t\tprop18='{$prop18}',\r\n\t\t\tprop19='{$prop19}',\r\n\t\t\tprop20='{$prop20}', \r\n\t\t\tfileurl='{$fileurl}'\r\n\t\t\twhere id='{$id}'\r\n\t\t" ); 454行代码注释, 新增457行代码: //新增下载 $msql->query( "insert into {P}_product_con set\r\n\t\tcatid='{$catid}',\r\n\t\tcatpath='{$catpath}',\r\n\t\ttitle='{$title}',\r\n\t\tbody='{$body}',\r\n\t\tdtime='{$dtime}',\r\n\t\txuhao='0',\r\n\t\tcl='0',\r\n\t\ttj='0',\r\n\t\tiffb='1',\r\n\t\tifbold='0',\r\n\t\tifred='0',\r\n\t\ttype='gif',\r\n\t\tsrc='{$src}',\r\n\t\tuptime='{$dtime}',\r\n\t\tauthor='{$author}',\r\n\t\tsource='{$source}',\r\n\t\tmemberid='0',\r\n\t\tproj='{$projpath}',\r\n\t\ttags='{$tagstr}',\r\n\t\tsecure='0',\r\n\t\tmemo='{$memo}',\r\n\t\tprop1='{$prop1}',\r\n\t\tprop2='{$prop2}',\r\n\t\tprop3='{$prop3}',\r\n\t\tprop4='{$prop4}',\r\n\t\tprop5='{$prop5}',\r\n\t\tprop6='{$prop6}',\r\n\t\tprop7='{$prop7}',\r\n\t\tprop8='{$prop8}',\r\n\t\tprop9='{$prop9}',\r\n\t\tprop10='{$prop10}',\r\n\t\tprop11='{$prop11}',\r\n\t\tprop12='{$prop12}',\r\n\t\tprop13='{$prop13}',\r\n\t\tprop14='{$prop14}',\r\n\t\tprop15='{$prop15}',\r\n\t\tprop16='{$prop16}',\r\n\t\tprop17='{$prop17}',\r\n\t\tprop18='{$prop18}',\r\n\t\tprop19='{$prop19}',\r\n\t\tprop20='{$prop20}'\r\n\t\t,\r\n\t\t\tfileurl='{$fileurl}'\r\n\t\t" ); 4:product\admin\func\upload.inc.php页面, 新增函数: 这个函数可以修改上传文件类型 ///////////上传文件函数 function NewUploadFile($jpg,$jpg_type,$fname,$jpg_size,$path){ global $strDownNotice9,$strDownNotice11; if ($jpg_size == 0) { $arr[0]="err"; $arr[1]=$strDownNotice9; return $arr; } if (substr($fname,-4)!=".rar" && substr($fname,-4)!=".zip" && substr($fname,-4)!=".doc" && substr($fname,-4)!=".xls" && substr($fname,-4)!=".htm" && substr($fname,-5)!=".html" && substr($fname,-4)!=".gif" && substr($fname,-4)!=".jpg" && substr($fname,-4)!=".png" && substr($fname,-4)!=".chm" && substr($fname,-4)!=".txt") { $arr[0]="err"; $arr[1]=$strDownNotice11; return $arr; } $hzarr=explode(".",$fname); $num=sizeof($hzarr)-1; $UploadImage[2]=$hzarr[$num]; $timestr=time(); $hz=substr($fname,-4); $file_path = ROOTPATH.$path."/".$timestr.$hz; $UploadImage[3] = $path."/".$timestr.$hz; copy ($jpg,$file_path); chmod ($file_path,0666); $UploadImage[0]="OK"; $UploadImage[1]="OK"; return $UploadImage; } 5:product\module\ProductQuery.php页面, 新增 第138行:$fileurl='/'.$fsql->f('fileurl');//新增文件上传功能 第199行:'fileurl' => $fileurl, //新增文件上传功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值