php使用 xlswriter 模塊導(dǎo)出數(shù)據(jù)

一、優(yōu)勢(shì):導(dǎo)出時(shí)間短;

二、下載擴(kuò)展
1、鏈接:https://pecl.php.net/package/xlswriter/1.2.3/windows
根據(jù)自己環(huán)境下載即可;

三、安裝庫(kù)文件
composer命令:composer require viest/php-ext-xlswriter-ide-helper:dev-master
或者直接下載包放在vendor目錄下(通過(guò)composer下載不了的情況) :
https://github.com/viest/php-ext-xlswriter-ide-helper

代碼示例

/**
    *
    * 導(dǎo)出EXCEL數(shù)據(jù)通用方法
    * @author fff
    * @column 單元格頭
    * @data   導(dǎo)出數(shù)據(jù)
    * @name   導(dǎo)出數(shù)據(jù)名稱
    *
    */
    public function downExcel(array $column, array $data, $name) {
        set_time_limit(0);
        ini_set('memory_limit', '10240M');
       //先處理為索引數(shù)組,不認(rèn)關(guān)聯(lián)數(shù)組;列名順序要與列值順序保持一致
        $new_data = [];
        foreach ($data as $key => $value) {
            $new_data[] = array_values($value);
        }
        $path   = app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR. 'storage'. DIRECTORY_SEPARATOR. 'exportExcel';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        $callStartTime = microtime(true);
        $config = ['path' => $path];                           //設(shè)置文件保存路徑
        $excel  = new \Vtiful\Kernel\Excel($config);

        // fileName 會(huì)自動(dòng)創(chuàng)建一個(gè)工作表,你可以自定義該工作表名稱,工作表名稱為可選參數(shù)
        $filePath = $excel->fileName($name . '.xlsx', 'Sheet1')
            ->header($column) // 設(shè)置表首行名稱
            ->data($new_data)
            ->output();
        if (!empty($filePath)) {
            return download($filePath, $name . '.xlsx');
        } else {
            return errorJson('導(dǎo)出數(shù)量超過(guò)30W條,無(wú)法下載');
        }

    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容