PHPSpreadsheet是一个用纯php编写的电子表格操作库,可以读取和写入不同的电子表格文件格式,如excel和libreoffice calc。Github地址为:https://github.com/PHPOffice/PhpSpreadsheet
支持的文件类型:
文件格式 | 读操作 | 写操作 |
Open Document Format/OASIS (.ods) | ||
Office Open XML (.xlsx) Excel 2007 and above | ||
BIFF 8 (.xls) Excel 97 and above | ||
BIFF 5 (.xls) Excel 95 | ||
SpreadsheetML (.xml) Excel 2003 | ||
Gnumeric | ||
HTML | ||
SYLK | ||
CSV | ||
PDF (using either the TCPDF, Dompdf or mPDF libraries, which need to be installed separately) |
1. 安装环境与依赖需求
PHP version 5.6 or newer
PHP extension php_zip enabled
PHP extension php_xml enabled
PHP extension php_gd2 enabled (if not compiled in)
2.安装
使用composer 安装PHPSpreadsheet文件库到相应的文件夹中:
composer require phpoffice/phpspreadsheet
3.HelloWorld 示例
<?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx');
4. 更多应用示例
php -S localhost:8000 -t vendor/phpoffice/phpspreadsheet/samples
可以访问http://localhost:8000/进行浏览查看。