thinkphp如何使用phpexcel导出Excel数据表
thinkphp如何使用phpexcel导出Excel数据表

thinkphp如何使用phpexcel导出Excel数据表

1.下载phpexcel类库文件,放在extend下面。

2.php

首先引入类库:

import('phpexcel.PHPExcel',EXTEND_PATH);

import('phpexcel.PHPExcel.Worksheet.Drawing',EXTEND_PATH);
import('phpexcel.PHPExcel.Writer.Excel2007',EXTEND_PATH);

 public function daochu(){
       $data = "查询出数组";
        $objExcel = new /PHPExcel();
        //set document Property
        $objWriter = /PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        $objActSheet = $objExcel->getActiveSheet();
        $key = ord("A");
        $letter =explode(',',"A,B,C,D,E,F,G,H");
        $arrHeader =  array('时间','店铺id','店铺名称','分店名称','销售量','销售额','应打款金额','状态');;
        //填充表头信息
        $lenth =  count($arrHeader);
        for($i = 0;$i < $lenth;$i++) {
            $objActSheet->setCellValue("$letter[$i]1","$arrHeader[$i]");
        };
        //填充表格信息
        foreach($data as $k=>$v){
            $k +=2;
            $objActSheet->setCellValue('A'.$k,$v['gap']);
            $objActSheet->setCellValue('B'.$k, $v['store_id']);
            // // 图片生成
            // $objDrawing[$k] = new /PHPExcel_Worksheet_Drawing();
            // $objDrawing[$k]->setPath('public/static/admin/images/profile_small.jpg');
            // // 设置宽度高度
            // $objDrawing[$k]->setHeight(40);//照片高度
            // $objDrawing[$k]->setWidth(40); //照片宽度
            // /*设置图片要插入的单元格*/
            // $objDrawing[$k]->setCoordinates('C'.$k);
            // // 图片偏移距离
            // $objDrawing[$k]->setOffsetX(30);
            // $objDrawing[$k]->setOffsetY(12);
            // $objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
            // 表格内容
            $objActSheet->setCellValue('C'.$k, $v['store_name']);
            $objActSheet->setCellValue('D'.$k, $v['branch']);
            $objActSheet->setCellValue('E'.$k, $v['snum']);
            $objActSheet->setCellValue('F'.$k, $v['pay_ables']);
            $objActSheet->setCellValue('G'.$k, $v[0]);
            $objActSheet->setCellValue('H'.$k, $v['storetrade_status']);

            // 表格高度
            $objActSheet->getRowDimension($k)->setRowHeight(20);
        }

        $width = array(20,20,15,10,10,30,10,15);
        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth($width[0]);
        $objActSheet->getColumnDimension('B')->setWidth($width[3]);
        $objActSheet->getColumnDimension('C')->setWidth($width[5]);
        $objActSheet->getColumnDimension('D')->setWidth($width[3]);
        $objActSheet->getColumnDimension('E')->setWidth($width[3]);
        $objActSheet->getColumnDimension('F')->setWidth($width[3]);
        $objActSheet->getColumnDimension('G')->setWidth($width[3]);
        $objActSheet->getColumnDimension('H')->setWidth($width[5]);

        $outfile = "商家交易结算".date("Y-m-d").".xls";
        ob_end_clean();
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$outfile.'"');
        header("Content-Transfer-Encoding: binary");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }

3.静态页面内容

<div>
    <a style="text-decoration:none"  href="{:url('daochu')}" class="btn btn-info radius" id="daochu"><i class="fa fa-search"></i>导出数据</a>
    </div>

https://blog.csdn.net/BenChiZhuBaDaoWang/article/details/125186751

发表回复

您的电子邮箱地址不会被公开。