一、優(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ú)法下載');
}
}