学无先后,达者为师

网站首页 PHP其他 正文

php运用C拓展excel高效大数据量导出

作者:渡目成书 更新时间: 2022-01-28 PHP其他
  • 导出excel功能在一些需要报表的系统中是非常常见的功能,在一般数据量较少的情况下phpexcel能够完美解决我们的问题,并且能够对表格做很多例如颜色,字体,单元格等处理

  • 但是在数据量大的情况下,phpexcel的性能下降异常厉害,内存需求很大,耗时很长,基本无法使用,前面使用fputcsv函数进行导出处理解决了大数据量导出的问题,但是无法对数据进行更多的处理

  • 今天发现了一个C拓展的excel处理包,能够高性能还可以处理表格数据,就来使用一下

  • 首先安装 拓展,详细安装(略)
    在这里插入图片描述

  • 现在就在laravel框架中进行测试,php版本7.2.14

public function tt(){
        ini_set("memory_limit", "2048M");
        set_time_limit(0);
        $test = Bp::where('server_time','>',1600041600)->where('server_time','<',1600085700)->get();
//        $test = Bp::take(1000)->get();
        $count = $test->count();
        $m1 = memory_get_usage();
        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];
        $t1 = microtime(true);
        //执行逻辑
        $datas=[];
        foreach ($test as $item){
            $datas[]=[$item->hwid,$item->server_time];
        }
        $config = ['path' =>public_path()];
        /*$excel  = new \Vtiful\Kernel\Excel($config);
        $filePath = $excel->fileName('ext.xlsx', 'sheet1')
            ->data($datas)
            ->output();*/

        $excel  = new \Vtiful\Kernel\Excel($config);

        $fileObject = $excel->constMemory('tutorial01.xlsx');
        $fileHandle = $fileObject->getHandle();

        $format    = new \Vtiful\Kernel\Format($fileHandle);
        $boldStyle = $format->bold()->toResource();

        $fileObject->setRow('A1', 10, $boldStyle) // 写入数据前设置行样式
            ->data($datas)
            ->output();
        $t2 = microtime(true);
        $m2 = memory_get_usage();
        $t = number_format($t2-$t1,2).'s';
        $size = $m2-$m1;
        $size = round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
        return ['type'=>'ext','time'=>$t,'memory'=>$size,'count'=>$count];
    }

在这里插入图片描述

  • 导出的速度还是比较不错的

  • 并且支持单元格操作等,还能导出图表也是一大亮点
    在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_43674113/article/details/108622832

栏目分类
最近更新