PhpSpreadsheet是一個(gè)PHP表格文件處理庫(kù),可用來(lái)讀寫excel文件,本文介紹如何在禪道中引入PhpSpreadsheet庫(kù),可以使用它來(lái)解析Excel文件,比如上傳excel格式的測(cè)試用例、導(dǎo)出測(cè)試用例為excel格式等。
安裝PhpSpreadsheetPhpSpreadsheet要求PHP 7.3及以上版本,如果禪道使用的PHP版本低于7.3請(qǐng)不要使用它來(lái)處理excel格式文件。
由于禪道安裝在centos7中,這里介紹如何在centos7中安裝PhpSpreadsheet,另外順便也記錄一下在windows系統(tǒng)中安裝phpspreadsheet的步驟。
linux安裝這里介紹centos7中如何安裝PhpSpreadsheet到禪道目錄下。
1. 安裝compor需要使用compor進(jìn)行安裝PhpSpreadsheet,先安裝一下compor工具。
安裝:
# 下載compor$ curl -sS https://getcompor.org/installer | phpAll ttings correct for using ComporDownloading...Compor (version 2.2.7) successfully installed to: /root/compor.pharU it: php compor.phar# 將compor.phar文件移動(dòng)到bin目錄以便全局使用compor命令$ mv compor.phar /usr/local/bin/compor# 切換國(guó)內(nèi)源$ compor config -g repo.packagist compor https://mirrors.aliyun.com/compor/2. 卸載低版本PHP
卸載原來(lái)的低版本PHP:
$ rpm -qa|grep phpphp-cli-5.4.16-48.el7.x86_64php-common-5.4.16-48.el7.x86_64php-5.4.16-48.el7.x86_64$ rpm -e php-cli-5.4.16-48.el7.x86_64錯(cuò)誤:依賴檢測(cè)失敗: php-cli(x86-64) = 5.4.16-48.el7 被 (已安裝) php-5.4.16-48.el7.x86_64 需要$ rpm -e php-5.4.16-48.el7.x86_64警告:/etc/httpd/conf.d/php.conf 已另存為 /etc/httpd/conf.d/php.conf.rpmsave$ rpm -e php-cli-5.4.16-48.el7.x86_64$ rpm -e php-common-5.4.16-48.el7.x86_64
將禪道的PHP 加入環(huán)境變量:
vi /etc/profileexport PATH="$PATH:/opt/zbox/bin/"source /etc/profile3. 安裝phpspreadsheet
進(jìn)入禪道目錄安裝phpspreadsheet:
$ cd /opt/zbox/app/zentao/lib$ mkdir excel$ cd excel$ compor require phpoffice/phpspreadsheet
會(huì)自動(dòng)安裝到vendor目錄下:
$ pwd/opt/zbox/app/zentao/lib/excel/vendor$ lsautoload.php compor ezyang maennchen markbaker myclabs phpoffice psr symfonywindows安裝
我使用的是XAMPP軟件包配置的PHP環(huán)境,和linux中一樣,先安裝PHP依賴管理工具compor。
1. 安裝compor下載phar文件 compor.phar:https://getcompor.org/download/,復(fù)制到D: oolsxamppphppear 目錄下,創(chuàng)建 compor.bat 文件,編寫代碼:
@php %~dp0compor.phar %*
然后將 D: oolsxamppphppear 目錄添加到環(huán)境變量。
打開(kāi)cmd窗口,執(zhí)行 compor -V 查看是否安裝成功:
$ compor -VCompor version 2.3.3 2022-04-01 22:15:35
也可以下載 Compor-Setup.exe 文件進(jìn)行安裝。
2. 安裝phpspreadsheet進(jìn)入 D: oolsxamppphppear 目錄下,執(zhí)行:
compor require phpoffice/phpspreadsheet
如果報(bào)如下錯(cuò)誤:
Installation failed, deleting ./compor.json.In RequireCommand.php line 210: No compor.json prent in the current directory (./compor.json), this may be the cau of the following excepti on.In PackageDiscoveryTrait.php line 308: Package phpoffice/phpspreadsheet has requirements incompatible with your PHP version, PHP extensions and Compor v ersion: - phpoffice/phpspreadsheet 1.22.0 requires ext-gd * but it is not prent.
修改 D: oolsxamppphpphp.ini 文件,取消 extension=gd 和 extension=fileinfo 的注釋。
安裝完成后執(zhí)行 compor info 可看到安裝的 phpoffice/phpspreadsheet 包。
禪道集成PhpSpreadsheetPhpSpreadsheet安裝到禪道目錄后就可以使用它來(lái)處理excel文件了,先在module目錄下創(chuàng)建模塊excel,然后在/module/excel 創(chuàng)建文件model.php,編寫讀取Excel文件方法:
<?phprequire '/opt/zbox/app/zentao/lib/excel/vendor/autoload.php';u PhpOfficePhpSpreadsheetIOFactory;u PhpOfficePhpSpreadsheetReaderXlsx;u PhpOfficePhpSpreadsheetReaderXls;u PhpOfficePhpSpreadsheetCellCoordinate;class excelModel extends model{ /** * Par Excel. * * @param string $fileName * @access public * @return void */ public function readexcel($fileName) { // $reader = new Xlsx(); // $spreadsheet = $reader->load($fileName); $reader = IOFactory::createReader('Xlsx'); $spreadsheet = $reader->load($fileName); // $datas = $spreadsheet->getActiveSheet()->toArray(); $worksheet = $spreadsheet->getActiveSheet(); $worksheetArray = $worksheet->toArray(); }}
PhpSpreadsheet的更多使用方法可參考官方文檔:https://phpspreadsheet.readthedocs.io/en/latest/ 。
--THE END--
起風(fēng)了,要努力活下去嗎,不,無(wú)須如此。——堀辰雄《起風(fēng)了》
本文發(fā)布于:2023-02-28 20:58:00,感謝您對(duì)本站的認(rèn)可!
本文鏈接:http://m.newhan.cn/zhishi/a/167771191395054.html
版權(quán)聲明:本站內(nèi)容均來(lái)自互聯(lián)網(wǎng),僅供演示用,請(qǐng)勿用于商業(yè)和其他非法用途。如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除。
本文word下載地址:spreadsheet(spreadsheet compare).doc
本文 PDF 下載地址:spreadsheet(spreadsheet compare).pdf
| 留言與評(píng)論(共有 0 條評(píng)論) |