PHP code example of ang3 / php-excel-encoder

1. Go to this page and download the library: Download ang3/php-excel-encoder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ang3 / php-excel-encoder example snippets




ng3\Component\Serializer\Encoder\ExcelEncoder;

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


// 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);

// 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"
//    }
//  }
// }

$data = $encoder->decode('my_excel_file.xlsx', ExcelEncoder::XLS, [
    ExcelEncoder::AS_COLLECTION_KEY => false
]);
shell
composer 

// 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"
//    }
//  }
// }
$ git clone https://github.com/Ang3/php-excel-encoder.git