需要安裝COMPOSER,可參考下面文章
如何在 CentOS 8 上安裝和使用 PHP Composer
演示圖:
PHP PhpSpreadsheet 製作輸出 EXCEL 檔案
PhpSpreadsheet 是一個用純 PHP 編寫的庫,它提供了一組允許您讀取和寫入各種電子表格文件格式(如 Excel 和 LibreOffice Calc)的類。
支持的文件格式[td]格式 | 閱讀 | 寫作 | 開放文檔格式/OASIS (.ods) | ✓ | ✓ | Office Open XML (.xlsx) Excel 2007 及更高版本 | ✓ | ✓ | BIFF 8 (.xls) Excel 97 and above | ✓ | ✓ | BIFF 5 (.xls) Excel 95 | ✓ |
| SpreadsheetML (.xml) Excel 2003 | ✓ |
| 數字 | ✓ |
| HTML | ✓ | ✓ | SYLK | ✓ |
| CSV文件 | ✓ | ✓ | PDF(使用需要單獨安裝的 TCPDF、Dompdf 或 mPDF 庫) |
| ✓ | 入門
軟件要求
使用 PhpSpreadsheet 開發的 PHP 7.4 或更新版本。其他要求,例如 PHP 擴展,由 composer 執行。有關詳細信息,請參閱composer.json 文件require的部分 。
PHP版本支持
LTS:對 PHP 版本的支持只會在該 PHP 版本生命週期結束後的六個月內得到維護 。
目前所需的 PHP 最低版本是 PHP 7.4:最後一個版本是 2022 年 9 月 29 日的 7.4.32,安全支持將於 2022 年 11 月 28 日結束,因此 PhpSpreadsheet 將支持 PHP 7.4 直到 2023 年 5 月 28 日。PHP 8.0 正式結束生命週期2023 年 11 月 26 日,PhpSpreadsheet 將在該日期後的六個月內繼續支持 PHP 8.0。
有關其他要求,請參閱composer.json。
安裝
使用composer將 PhpSpreadsheet 安裝到你的項目中:
登入到網頁到終端機輸入
- composer require phpoffice/phpspreadsheet
複製代碼
如果您打算使用它們,也可以下載文檔和示例:
- composer require phpoffice/phpspreadsheet --prefer-source
複製代碼
php-fpm如果您在開發機器上構建您的安裝,而該開發機器的 PHP 版本與將要部署的服務器不同,或者如果您的 PHP CLI 版本與您的運行時(例如Apache 的)不同,那麼您可能mod_php需要在安裝之前將以下內容添加到您的composer.json:
- {
- "require": {
- "phpoffice/phpspreadsheet": "^1.23"
- }
- "config": {
- "platform": {
- "php": "7.4"
- }
- }
- }
複製代碼
然後運行
以確保檢索到正確的依賴項以匹配您的部署環境。
有關更多詳細信息,請參閱CLI 與應用程序運行時。
輸出 Hello World
這將是編寫電子表格的最簡單方法:
- <?php
- require 'vendor/autoload.php';
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- $spreadsheet = new Spreadsheet();
- $activeWorksheet = $spreadsheet->getActiveSheet();
- $activeWorksheet->setCellValue('A1', 'Hello World !');
- $writer = new Xlsx($spreadsheet);
- $writer->save('hello world.xlsx');
複製代碼
到FTP下載hello world.xlsx 檔案即可看到
參考文章
https://phpspreadsheet.readthedocs.io/en/latest/
|