samagtech / excel-lib
Requires
- phpoffice/phpspreadsheet: ^1.18
Requires (Dev)
- phpunit/phpunit: ^9.5
README
前言
这个库是 PHPSpreadSheet 的封装,PHPExcel 的高级版本,没有 PHP 7.4+ 的问题,并且可以通过以下方式安装:
composer require samagtech/excel-lib
在修改库和/或添加功能时,建议使用命令运行/编写单元测试以检查函数的完整性。
composer test
要安装 PHPUnit (文档),请运行以下命令:
composer install --dev
此外,为了处理库的错误,需要处理 ExcelException 异常。
创建 Excel 工作表
要创建 Excel,必须以这种方式实例化一个 Writer 对象:
$writer = new \SamagTech\ExcelLib\Writer($path);
或者
$writer = (new \SamagTech\ExcelLib\Factory())->createWriter($path);
Writer 需要一个 path 来保存文件,但也接受文件名和要忽略格式的列的列表。创建示例
use SamagTech\ExcelLib\Writer; $writer = new Writer($path, $filename, $ignoreFieldsFormat); or $writer = new Writer($path, ignoreFieldsFormat: $ignoreFieldsFormat); or $writer = new Writer($path); $writer->setFilename($filename)->setIgnoreFieldsFormat($ignoreFieldsFormat);
也可以在运行时更改 path
$writer->setPath($path);
Writer 的使用
基本使用示例
$filePath = $writer->setHeader($headers)->setBody($body)->build();
setHeader() 函数设置工作表标题,是可选的,setBody() 函数设置要插入工作表的数据,build() 函数构建工作表并返回文件名和路径。
定义列格式
使用 setColumnDefinition(array $columnDefinition) 函数可以定义列的格式,其中键是列的键,值是格式类型。
$columnDefinition = [ 'name' => 'string', 'age' => 'number', 'perc_fat' => 'percentage' ];
NB. 目前只支持数字、字符串和百分比,默认情况下只应用字符串和数字逻辑(数字将自动格式化为意大利格式 X.XXX,XX,如果为负数则显示为红色)
使用多工作表构建 Excel
可以使用 build() 方法构建具有多个工作表的 Excel 文件,通过传递参数 true,将自动处理作为多工作表的身体。多工作表构建示例
$body = [ // Foglio 1 'sheet_1' => [ // Riga 1 [ // Dati ], // Riga 2 [ // Dati ] ], // Foglio 2 'sheet_2' => [ // Riga 1 [ // Dati ], // Riga 2 [ // Dati ] ] ]; $filePath = $writer->setBody($body)->build(true);
关于标题(始终可选)
$header = [ // Intestazione foglio 1 [ // Stringhe che diverranno colonne ], // Intestazione foglio 2 [ // Stringhe che diverranno colonne ] ] or // In questo caso non essendo diviso l'intestazione verrà duplicata per ogni foglio $header = [ // Stringhe che diverranno colonne ]
读取 Excel 工作表
要读取 Excel,必须以这种方式实例化一个 Reader 对象:
$reader = new \SamagTech\ExcelLib\Reader($path, $filename);
或者
$reader = (new \SamagTech\ExcelLib\Factory())->createReader($path, $filename);
Reader 需要一个 path 和 filename 来加载文件。文件名和路径可以在运行时更改。
$reader->setPath($path) or $reader->setFilename($filename)
Reader 的使用
基本使用示例
$array = $reader->toArray(); or $object = $reader->toObject();
因此,可以将 Excel 转换为对象或数组。如果正在加载的文件包含多个工作表,则按以下方式加载工作表:
// Excel con foglio 1 e 2 $array = $reader->toArray(); // L'array sarà strutturato con il nome dei fogli dove gli spazi verranno modificati in underscore /** * $array = [ * 'Foglio_1' => [ * [ * // Riga 1 * ], * [ * // Riga 2 * ] * ], * 'Foglio_2' => [ * [ * // Riga 1 * ], * [ * // Riga 2 * ] * ] * * * /
基于列自定义键
可以为转换列和自定义键提供自定义定义
$customColumnToKey = [ 'Nome' => 'firstname', 'Cognome' => 'lastname', 'Età' => 'age' ] $data = $reader->setColumnToKey($customColumnToKey)->toArray(); /** * Es. Excel * * Nome | Cognome | Età * Alessandro | Marotta | 25 * * $data = [ * [ * 'firstname' => 'Alessandro', * 'lastname' => 'Marotta', * 'age' => 25, * ], * // Altre righe * ] * * /
NB. 在这种情况下,将忽略未定义的列。需要在工作表中提供列标题。
读取特定工作表
可以隐式指定要加载的工作表
$sheetNames = 'Foglio 1'; // or $sheetNames = ['Foglio 1', 'Foglio 2'] $reader->setSheetNames($sheetNames)->toArray();
指示工作表没有标题
可能工作表没有标题,因此最好指出来
$reader->setHasHeader(false)->toArray();
Reader 的有用功能
getNumSheet() - 返回实际加载的工作表数量