PHP環境下的導出功能優化改造,單次同步導出數據量在2萬之上。 原有導出功能使用的PHP5.6+PHPExcel.這一次換成了PHP7.2+php-ext-excel-export。php
官方github地址 https://github.com/viest/php-ext-excel-exportgit
安裝擴展失敗的追蹤記錄github
官方安裝擴展的建議web
Liunx 下使用數據庫
pecl install xls-writer
PHP 7.2.13 (cli) (built: Dec 8 2018 12:27:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologiescentos
CentOS 6.7數組
sudo make && make install In file included from /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:186: /usr/local/src/php-ext-excel-export/library/third_party/minizip/crypt.h:35: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token /usr/local/src/php-ext-excel-export/library/third_party/minizip/crypt.h:48: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token /usr/local/src/php-ext-excel-export/library/third_party/minizip/crypt.h:65: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token /usr/local/src/php-ext-excel-export/library/third_party/minizip/crypt.h:94: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c: In function ‘zipOpenNewFileInZip4_64’: /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1245: error: ‘curfile64_info’ has no member named ‘crypt_header_size’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1251: error: ‘curfile64_info’ has no member named ‘pcrc_32_tab’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1254: error: ‘curfile64_info’ has no member named ‘pcrc_32_tab’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1255: error: ‘curfile64_info’ has no member named ‘crypt_header_size’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c: In function ‘zip64FlushWriteBuffer’: /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1375: error: ‘curfile64_info’ has no member named ‘pcrc_32_tab’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1375: error: ‘curfile64_info’ has no member named ‘pcrc_32_tab’ /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c: In function ‘zipCloseFileInZipRaw64’: /usr/local/src/php-ext-excel-export/library/third_party/minizip/zip.c:1610: error: ‘curfile64_info’ has no member named ‘crypt_header_size’ make: *** [library/third_party/minizip/zip.lo] Error 1
在github 以Issues(https://github.com/viest/php-ext-excel-export/issues/139
)的方式與官方溝通後,你們得出兩個結論服務器
1 能夠嘗試使用 yum install php-pecl-xlswriter 解決異步
yum install php-pecl-xlswriter
2 編譯報錯是由於服務器centos版本太低形成性能
貢獻者回復
This problem only occurs in CentOS6, because CentOS6 is too old.
對於數據導出功能的開發,這裏主要說同步導出,對於數據同步導出功能有幾個關鍵路徑。
1 無論使用哪一種導出擴展或者組件,數據都有一個從新組裝的過程,應該儘可能減小這個過程當中的數組循環次數和數據庫或者第三方服務的交互次數。屢次循環的性能浪費會觸碰到PHP的性能底線,如運行超時。
2 對於業務方來講,多數但願一次導出一個週期內的數據,好比一個月或者3個月。若是數據的來源是直接訪問數據庫而來,那麼有一點很是重要,這裏的數據庫訪問SQL必定要採用分頁的形式,每頁的pagesize能夠比較大,好比3000,或者5000。切忌採用一次按照查詢條件查詢出來全部數據,這樣在生產環境中確定是一個隱患,運行一段時間後,總會遇到瓶頸。
php-ext-excel-export這個擴展的性能仍是能夠的,驗證過同步導出5-6萬的數據還能夠應付。
固然了,對於web頁面導出這樣的功能,若是我是產品經理,我更傾向與使用異步方式,以報表箱的形式呈現結果。你要問我緣由,留個懸念,我會單獨寫個總結文章。
sudo yum install php-pecl-xlswriter Total 968 kB/s | 147 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : libxlsxwriter-0.8.7-1.el6.remi.x86_64 1/2 Installing : php-pecl-xlswriter-1.2.5-1.el6.remi.7.2.x86_64 2/2 Verifying : php-pecl-xlswriter-1.2.5-1.el6.remi.7.2.x86_64 1/2 Verifying : libxlsxwriter-0.8.7-1.el6.remi.x86_64 2/2 Installed: php-pecl-xlswriter.x86_64 0:1.2.5-1.el6.remi.7.2 Dependency Installed: libxlsxwriter.x86_64 0:0.8.7-1.el6.remi
渲染數據,設置對齊方式,設置頭部粗體
/** * 渲染數據 * * @param mixed $header excel標題欄 * @param mixed $data 數組 * * @return mixed */ public function make($header, $data) { /* 適用於新擴展1.2.5 start*/ $fileHandle = $this->handle->getHandle(); $format = new \Vtiful\Kernel\Format($fileHandle); $alignStyle = $format ->align(Format::FORMAT_ALIGN_LEFT, Format::FORMAT_ALIGN_LEFT) ->toResource(); $this->handle->header($header) ->data($data) ->setRow('A1', 30, $boldStyle)->setRow('A1', 30, $alignStyle) ->output(); } /** * 導出 * * @return mixed */ public function output() { $res = $this->handle->output(); if ($res) { return ['root' => $this->filePath, 'file' => $this->fileName]; } }
php --ri xlswriter show xlswriter support => enabled Version => 1.2.5 libxlsxwriter headers version => 0.8.7 libxlsxwriter library version => 0.8.7
pear is php package management.
pecl is php extension management.
php -m |grep xlswriter
https://github.com/viest/php-ext-excel-export/issues/134#issuecomment-507160228
https://github.com/viest/php-ext-excel-export/issues/139
文章已同步到公衆號《圖南日晟》歡迎關注