首页 > 编程语言 >PHP导出Excel,从xlswriter到golang的进化是2分缩减到5秒

PHP导出Excel,从xlswriter到golang的进化是2分缩减到5秒

时间:2024-02-04 11:46:06浏览次数:23  
标签:return Excel list value xlswriter golang header 导出 fileObject

先看图

 

一、介绍

xlswriter是一个高效处理excel文件的PHP扩展,底层以C语言实现;处理速度是PHPExcel几十倍甚至几百倍的效率。

官方链接:https://gitee.com/viest/php-ext-xlswriter

缺点:更深入的功能(例如读取excel图片)健全;导出excel样式不够丰富

导出excel文件,xlswriter绝对是效率最高的处理方式,配合php后台任务导出百万千万级别数据也没有问题

通用导出类:

class ExportOperateAnaly
{
    public $export_limit = 13000;

    /**
     * 统一导出
     * @Author   zhibin3
     * @DateTime 2022-12-24
     * @param    array      $pageData [description]
     * @return   [type]               [description]
     */
    public function layout(array $params, $is_export=true)
    {
        ini_set('memory_limit', '512M');

        
        $header_list = ['商品ID', '商品标题', '商品图片'];
        $width_list = ['商品图片'=>55,'商品标题'=>55];

        return $this->export($header_list, $list, '商品运营分析', $is_export, $width_list);
    }

    /**
    * 通用导出方法
    * @Author   zhibin3
    * @DateTime 2023-02-14
    * @param    array      $header_list [description]
    * @param    array      $list        [description]
    * @return   [type]                  [description]
    */
   public function export(array $header_list, array $datas, $filename='选款导出列表', $is_export=true, array $width_list=[])
   {
        $config = [
            'path' => ROOT_PATH . 'public' . DS . 'dl' // xlsx文件保存路径
        ];
        $Excel = new \Vtiful\Kernel\Excel($config);

        $filename .= datetime(time(), 'Y-m-d_H_i_s').'.xlsx';
        $fileObject = $Excel->fileName($filename, 'sheet1')->header($header_list);
        $fileHandle = $fileObject->getHandle();

        $format    = new \Vtiful\Kernel\Format($fileHandle);
        $boldStyle = $format->bold()->toResource();
        $alignStyle = $format->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();
        // $backgroundStyle  = $format->background(0xD9D9D9)->toResource();

        foreach ($datas as $index => $val) {
            $column = 0;
            foreach ($val as $key => $value) {
                $row = $index+1;
                /*if ($key=='img') {
                    $return = ImageImport::export_image_save($value);
                    $img_path = $return['image'] ? $return['path'] : $value;
                    // pre($value, $return['image'], $row, $column, $img_path);
                    $fileObject->insertImage($row, $column, $img_path, 0.1, 0.1);
                }*/

                if ($key=='lowest_price') {
                    $fileObject->insertFormula($row, $column, $value);
                }
                else if (is_string($value)) {
                    $fileObject->insertText($row, $column, $value);
                } else {
                    $fileObject->insertText($row, $column, $value);
                }

                $column++;
            }
        }
        // pre($datas);die;
        
        foreach ($header_list as $key => $value) {
            // $letter = $header_map[$key];
            $letter = \Vtiful\Kernel\Excel::stringFromColumnIndex($key);
            $width_value = $width_list[$value] ?? 20;
            $fileObject->setRow($letter.($key+1), 35, $boldStyle);
            $fileObject->setColumn($letter.':'.$letter, $width_value, $alignStyle);
        }

        $format2    = new \Vtiful\Kernel\Format($fileHandle);
        $alignStyle = $format2->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();
        $wrapStyle = $format2->wrap()->toResource();
        foreach ($datas as $index => $value) {
            $row = $index+2;
            $fileObject->setRow('A'.$row, 30, $alignStyle);  
        }
        // pre($fileObject);die;
        $filePath = $fileObject->output();
        if ($is_export) {
            header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            header('Content-Disposition: attachment;filename="' . $filename . '"');
            header('Content-Length: ' . filesize($filePath));
            header('Content-Transfer-Encoding: binary');
            header('Cache-Control: must-revalidate');
            header('Cache-Control: max-age=0');
            header('Pragma: public');

            if (ob_get_contents()) ob_clean();
            flush();
            if (@copy($filePath, 'php://output') === false) throw new \Exception($filePath. '地址出问题了');
            @unlink($filePath);

            return ['code'=>1, 'msg'=>'导出成功', 'file_path'=>''];
        } else {
            
            return ['code'=>1, 'msg'=>'导出成功', 'file_path'=>'dl/'.$filename];
        }   
    }

}

二、golang

 

标签:return,Excel,list,value,xlswriter,golang,header,导出,fileObject
From: https://www.cnblogs.com/cnxzb/p/18005886

相关文章

  • 利用POI清空excel文件
    就是说,删掉指定excel文件里的所有sheet页。 ......
  • 用VB6.0封装DLL组件并在EXCEL中调用
    使用程序:1、MicrosoftOfficeExcel20032、MicrosoftVisualBasic6.0 案例:在工作表的C1单元格得出A1单元格+B1单元格的值。设计的VBA代码:SubTest()  OnErrorResumeNext  Range("C1")=Cells(1,1)+Cells(1,2)EndSub  第一部分、使用VB6.0制作DL......
  • C# NPOI reflection import data into excel file
    usingSystem.ComponentModel.DataAnnotations;usingSystem.Diagnostics;usingSystem.Runtime.CompilerServices;usingSystem.Security.Cryptography;usingSystem.Text;usingNewtonsoft.Json;usingSystem.Reflection;usingNPOI.SS.Formula.Functions;usingNPO......
  • excel常见错误类型
    类型一:####错误原因:一般情况下单元格的宽度放不下所有的内容,也有可能是在日期格式列中输入负数的日期或者时间。解决方案:拉大列宽,修改格式为常规。类型二:DIV/0!错误原因:数据相除运算中除数(分母)为0或者为空格解决方案:查看除数是否出现0的情况是否合理,不合理就需要重新计算,合理......
  • Golang 开发Android服务--交叉编译
    Golang开发Android服务--交叉编译移动端主要arm架构,编译命令CGO_ENABLED=0GOOS=linuxGOARCH=armgobuild-ogo_promine.goCGO_ENABLED=0GOOS=linux,指定临时环境变量不影响其它编译,也可以设置为全局环境变量。AOSP查看版本信息getprop不传参数查看所有属性值ge......
  • Golang 向已关闭的chan读写数据会怎样
    1.向已关闭的chan写会直接panic,报sendtoclosechannel错误packagemainfuncmain(){ //创建缓冲区为4的字符串chan schoolChan:=make(chanstring,4) //放入3个值 schoolChan<-"国防科大" schoolChan<-"北京大学" schoolChan<-"湖南大学" //......
  • Golang中make和new的区别
    1.相同点都是内建函数,都是在堆上分配内存,都需要传递类型参数2.不同点传递的参数不一样,new函数只接收一个参数,make函数可以接收一个以上的参数packagemainimport"fmt"funcmain(){ //int类型0值的指针,返回的值是以0x开头的16进制整数,参数个数为1 intZeroValueP......
  • Java实现学生信息管理系统:从Excel中提取数据的实用方法
    在Java中实现学生信息管理系统并从Excel中读取数据,通常适用于以下场景:数据迁移和集成:如果你有一个现存的学生信息管理系统,该系统可能使用数据库或其他存储方式,但你想将这些数据迁移到新的系统。Excel文件可能提供了一种方便的方式来迁移这些数据,因为Excel文件格式易于查看和编辑。......
  • excel学习记录
    《相关案例详见博客园文件》1.记录整理、数据计算、数据分析、数据展现2.保存->工具->常规选项->设置打开密码和修改权限密码,修改权限密码表示可以修改,但是修改后不能保存3.快速填充ctrl+e:对数据拆分、重组、合并 例如:A列有邮箱数据,想提取出邮箱数据中的人名,在B列输入A1中的......
  • EasyExcel 读取单元格内图片
    EasyExcel读取单元格内图片1.需求介绍需要实现Excel数据批量导入,且Excel中某个单元格内存在图片,需要将图片解析上传,并与所在行数据进行绑定,具体数据如:2.具体实现2.1版本介绍easyexcel:3.3.2SpringBoot:2.7.1hutool:5.7.212.2解决思路参考hutoolExcelPicUtil工具类,通......