robotusers/cakephp-excel

Robotusers CakePHP Excel 插件

安装次数: 10,589

依赖项: 0

建议者: 0

安全性: 0

星标: 23

关注者: 6

分支: 10

公开问题: 2

类型:cakephp-plugin

0.7.0 2024-02-08 10:40 UTC

This package is auto-updated.

Last update: 2024-09-08 11:49:16 UTC


README

Latest Stable Version Total Downloads Software License Build Status codecov

CakePHP Excel 插件允许使用 CakePHP ORM 的强大功能来操作电子表格文件。此插件使用 PHPSpreadsheet 库构建,并能与多种类型的电子表格文件(Excel、csv 等)协同工作。

安装

composer require robotusers/cakephp-excel
//Application.php

public function bootstrap()
{
    ...
    $this->addPlugin('Robotusers/Excel');
}

使用插件

Excel 插件允许您以多种方式操作电子表格文件。最简单的用法是将您的电子表格数据加载到 CakePHP ORM 表中。

例如,我们正在加载一个包含一些记录数据的 Excel 文件。

use Robotusers/Excel/Registry;

$registry = Registry::instance();
$table = $registry->get('path/to/records.xlsx', 'Albums');

电子表格数据现在已加载到 CakePHP ORM 表中。

$row = $table->find()->first()->toArray();

//this is how a simple row looks like:
[
    '_row' => 1,
    'A' => 'Led Zeppelin',
    'B' => 'Led Zeppelin II',
    'C' => '1969'
]

每列代表一个属性。默认情况下,值是 string

您还可以将列映射到自定义属性和类型。

use Robotusers/Excel/Registry;

$registry = Registry::instance();
$table = $registry->get('path/to/records.xlsx', 'Albums', [
    'primaryKey' => 'id',
    'columnMap' => [
        'A' => 'band',
        'B' => 'album',
        'C' => 'year'
    ],
    'columnTypeMap' => [
        'C' => 'date'
    ]
]);

电子表格数据现在已加载到 CakePHP ORM 中,具有自定义属性和类型。

$row = $table->find()->first()->toArray();

//this is how a simple row looks like:
[
    'id' => 1,
    'band' => 'Led Zeppelin',
    'album' => 'Led Zeppelin II',
    'year' => object(Cake\I18n\Date) {
        'time' => '1969-01-01T00:00:00+00:00',
        'timezone' => 'UTC'
    }
]

您可能想要操作一些数据并将其写回 Excel 文件。这也同样可行。

$row = $table->newEntity([
    'band' => 'Genesis',
    'album' => 'Selling England by the Pound',
    'year' => '1973'
]);
$table->save($row);

现在新记录已保存,但 Excel 文件尚未更新。您必须调用 writeSpreadsheet() 方法

$table->writeSpreadsheet();

您还可能只想读取或写入一些行和列。

use Robotusers/Excel/Registry;

$table = $registry->get('path/to/records.xlsx', 'Albums', [
    'startRow' => 2,
    'endRow' => 3,
    'startColumn' => 'B',
    'endColumn' => 'B'
]);

$row = $table->find()->first()->toArray();

//this is how a simple row looks like:
[
    '_row' => 1,
    'B' => 'Machine Head'
]

请注意,_row 并不匹配实际的行索引。为了保留原始行索引,您必须使用 keepOriginalRows 选项。

use Robotusers/Excel/Registry;

$table = $registry->get('path/to/records.xlsx', 'Albums', [
    'startRow' => 2,
    'endRow' => 3,
    'startColumn' => 'B',
    'endColumn' => 'B',
    'keepOriginalRows' => true
]);

$row = $table->find()->first()->toArray();

//this is how a simple row looks like:
[
    '_row' => 2,
    'B' => 'Machine Head'
]

同样的原则也适用于写入文件。如果删除第二行,当 keepOriginalRowsfalse 时,结果 Excel 文件中第二行不会为空。如果您想保持行的一致性,必须将此选项设置为 true

行为

此插件提供了一种行为,可以添加到任何表中。

//AlbumsTable.php

public function initialize()
{
    $this->addBehavior('Robotusers/Excel.Excel', [
        'columnMap' => [
            'A' => 'band',
            'B' => 'album',
            'C' => 'year'
        ]
    ]);
}

如果您想将数据加载到表中,您必须设置一个工作表实例。

use Cake\Filesystem\File;

$file = new File('path/to/file.xls');
$spreadsheet = $table->getManager()->getSpreadsheet($file); // \PhpOffice\PhpSpreadsheet\Spreadsheet instance
$worksheet = $spreadsheet->getActiveSheet(); // \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet instance

$table->setWorksheet($worksheet)->readSpreadsheet();

现在您的表已用 Excel 数据填充。

如果您想将数据写回 Excel 文件,您必须设置一个文件。

$table->setFile($file)->writeSpreadsheet();

处理不同的表

您还可以将数据加载到任何表中。

use Robotusers\Excel\Excel\Manager;

$table = TableRegistry::get('SomeTable');
$manager = new Manager();

$file = new File('file.xlsx');
$spreadsheet = $manager->getSpreadsheet($file);
$worksheet = $spreadsheet->getActiveSheet();

$manager->read($worksheet, $table, [
    'columnMap' => [
        'A' => 'band',
        'B' => 'album',
        'C' => 'year'
    ]
]);

//manipulate your data...

//here you have to tell where properties should be placed
$manager->write($table, $worksheet, [
    'propertyMap' => [
        'band' => 'A',
        'album' => 'B',
        'year' => 'C'
    ]
]);
//to actually save the file you have to call save()
$writer = $manager->save($spreadsheet, $file);