ang3/php-excel-encoder

PHP Excel 编码器

v3.0.1 2023-02-16 10:47 UTC

README

Code Quality PHPUnit Tests Symfony Integration Latest Stable Version Total Downloads

借助组件 phpoffice/phpspreadsheet,对 xls/xlsx 文件等进行编码和解码。

此外,此组件还使用了来自 symfony/serializer 包的 Serializer 组件 - 请阅读文档以获取有关序列化器使用的更多信息。

摘要

安装

打开命令行,执行以下命令以下载此包的最新稳定版本

composer require ang3/php-excel-encoder

如果您在 Symfony 应用程序之外安装此组件,您必须在您的代码中要求 vendor/autoload.php 文件以启用 Composer 提供的类自动加载机制。请参阅此文章以获取更多详细信息。

升级

要从 1.x 升级到 2.x 或从 2.x 升级到 3.x,请阅读文件 UPGRADE-2.0.md

用法

创建编码器

<?php

require_once 'vendor/autoload.php';

use Ang3\Component\Serializer\Encoder\ExcelEncoder;

// Create the encoder with default context
$encoder = new ExcelEncoder($defaultContext = []);

上下文参数

  • ExcelEncoder::AS_COLLECTION_KEY (布尔值):将数据作为集合加载 [默认:true]
  • ExcelEncoder::FLATTENED_HEADERS_SEPARATOR_KEY (字符串):扁平化条目键的分隔符 [默认:.]
  • ExcelEncoder::HEADERS_IN_BOLD_KEY (布尔值):将标题加粗(仅编码)[默认:true]
  • ExcelEncoder::HEADERS_HORIZONTAL_ALIGNMENT_KEY (字符串):将标题加粗(仅编码:leftcenterright)[默认:center]
  • ExcelEncoder::COLUMNS_AUTOSIZE_KEY (布尔值):列自动调整大小功能(仅编码)[默认:true]
  • ExcelEncoder::COLUMNS_MAXSIZE_KEY (整数):列最大大小功能(仅编码)[默认:50]

编码

接受的格式

  • ExcelEncoder::XLS
  • ExcelEncoder::XLSX
<?php
// Create the encoder...

// Test data
$data = [
  // Array by sheet
  'My sheet' => [
    // Array by rows
    [
      'bool' => false,
      'int' => 1,
      'float' => 1.618,
      'string' => 'Hello',
      'object' => new DateTime('2000-01-01 13:37:00'),
      'array' => [
        'bool' => true,
        'int' => 3,
        'float' => 3.14,
        'string' => 'World',
        'object' => new DateTime('2000-01-01 13:37:00'),
        'array' => [
          'again'
        ]
      ],
    ],
  ]
];

// Encode data with specific format
$xls = $encoder->encode($data, ExcelEncoder::XLSX);

// Put the content in a file with format extension for example
file_put_contents('my_excel_file.xlsx', $xls);

解码

接受的格式

  • ExcelEncoder::XLS
  • ExcelEncoder::XLSX
// Create the encoder...

// Decode data with no specific format
$data = $encoder->decode('my_excel_file.xlsx', ExcelEncoder::XLS);

var_dump($data);

// Output:
// 
// array(1) {
//  ["Sheet_0"] => array(1) { // Name of the sheet
//    [0] => array(15) { // First row
//      ["bool"] => int(0) // First cell
//      ["int"] => int(1)
//      ["float"] => float(1.618)
//      ["string"] => string(5) "Hello"
//      ["object.date"] => string(26) "2000-01-01 13:37:00.000000"
//      ["object.timezone_type"] => int(3)
//      ["object.timezone"] => string(13) "Europe/Berlin"
//      ["array.bool"] => int(1)
//      ["array.int"] => int(3)
//      ["array.float"] => float(3.14)
//      ["array.string"] => string(5) "World"
//      ["array.object.date"] => string(26) "2000-01-01 13:37:00.000000"
//      ["array.object.timezone_type"] => int(3)
//      ["array.object.timezone"] => string(13) "Europe/Berlin"
//      ["array.array.0"] => string(5) "again"
//    }
//  }
// }

标题

默认情况下,编码器将数据作为集合加载:标题名称是每个数据的键。实际上,上下文参数 ExcelEncoder::AS_COLLECTION_KEY 的默认值是 true。要禁用此功能,请将上下文参数作为 false 传递。

$data = $encoder->decode('my_excel_file.xlsx', ExcelEncoder::XLS, [
    ExcelEncoder::AS_COLLECTION_KEY => false
]);
// Output:
// 
// array(1) {
//  ["Sheet_0"] => array(1) { // Name of the sheet
//    [0] => array(15) { // Headers row
//      0 => "bool" // First cell
//      1 => "int"
//      2 => "float"
//      3 => "string"
//      4 => "object.date"
//      5 => "object.timezone_type"
//      6 => "object.timezone"
//      7 => "array.bool"
//      8 => "array.int"
//      9 => "array.float"
//      10 => "array.string"
//      11 => "array.object.date"
//      12 => "array.object.timezone_type"
//      13 => "array.object.timezone"
//      14 => "array.array.0"
//    },
//    [1] => array(15) { // First data row
//      [0] => int(0) // First cell
//      [1] => int(1)
//      [2] => float(1.618)
//      [3] => string(5) "Hello"
//      [4] => string(26) "2000-01-01 13:37:00.000000"
//      [5] => int(3)
//      [6] => string(13) "Europe/Berlin"
//      [7] => int(1)
//      [8] => int(3)
//      [9] => float(3.14)
//      [10] => string(5) "World"
//      [11] => string(26) "2000-01-01 13:37:00.000000"
//      [12] => int(3)
//      [13] => string(13) "Europe/Berlin"
//      [14] => string(5) "again"
//    }
//  }
// }

Symfony 集成

编码器实现了序列化器组件的接口。要启用此编码器,只需将其配置为服务,并将以下内容添加到文件 config/services.yaml

# config/services.yaml
services:
  # ...
  Ang3\Component\Serializer\Encoder\ExcelEncoder: ~

运行测试

$ git clone https://github.com/Ang3/php-excel-encoder.git

$ composer install

$ vendor/bin/simple-phpunit

就这样!