myclabs/xport

Import/Export library for PHP

0.2.2 2014-02-13 09:03 UTC

This package is not auto-updated.

Last update: 2024-11-18 15:08:34 UTC


README

Build Status Coverage Status Scrutinizer Quality Score

Xport is an import/export library for PHP.

It is targeted to support the following formats:

  • Excel (xlsx and xls)
  • OpenOffice (ods - to be implemented)
  • PDF (to be implemented)
  • XML (to be implemented)

It provides an object model for different formats (spreadsheet, document, XML…) and a language based on YAML and Twig to map your data (arrays, objects, …) onto the model.

Spreadsheet

Simple example

Simple mapping file (YAML file):

sheets:
    # An empty sheet named "Home"
  - label: "Home"

    # Another sheet named "Contacts"
  - label: "Contacts"

    content:
    # Containing one table with 2 columns
      - type: VerticalTable
        columns:
          - "Name"
          - "Phone Number"
        lines:
          - foreach: "contacts as contact"
            do :
              - cells:
                - "{{ contact.name }}"
                - "{{ contact.phoneNumber }}"

Usage:

$modelBuilder = new SpreadsheetModelBuilder();
$export = new PHPExcelExporter();

$modelBuilder->bind('contacts', $contacts);

$export->export($modelBuilder->build('mapping.yml'), 'myFile.xslx');

The table will be filled with each item in the array $contacts.

The path configuration is a PropertyAccess path, e.g. the contact.phoneNumber path can resolve to $contact->getPhoneNumber() or $contact->phoneNumber.

Dynamic example

You can use the foreach expression to generate dynamic content.

You can also use Twig templating language.

Here is an example:

# Create one sheet per company
sheets:
  - foreach: companies as i => company
    do:
      - label: "{{ i + 1 }} - {{ company.name }}" # Twig expression, will result in (for example): "1 - My Company"
$modelBuilder = new SpreadsheetModelBuilder();
$export = new PHPExcelExporter();

$modelBuilder->bind('companies', $companies);

$export->export($modelBuilder->build(new YamlMappingReader('mapping.yml')), 'myFile.xslx');

Here is a more complete example:

sheets:

    # Create one sheet per company
  - foreach: companies as company
    do:

      - label: "{{ company.name }}"
        content:
            # One content(VerticalTable) per product, each, followed by an empty line

          - foreach: company.products as product
            do:

              - type: VerticalTable
                label: product.label
                columns:
                  - "Product"
                  - "Price"
                  - "Salesman"
                # One line per sale, each, preceded by an empty line
                lines:
                  - foreach: product.getSalesList() as sale
                    do:
                      -
                      - cells:
                          - "{{ product.name }}"
                          - "{{ sale.price }}"
                          - "{{ sale.salesman.name }}"

              - type: EmptyLine

Functions

Functions can be used in Twig expressions, and are defined as such:

$modelBuilder = new SpreadsheetModelBuilder();
$export = new PHPExcelExporter();

$modelBuilder->bindFunction('up', function($str) {
    return strtoupper($str);
});

$export->export($modelBuilder->build(new YamlMappingReader('mapping.yml')), 'myFile.xslx');

File format

You can choose which file format to use through PHPExcel writers:

// ...
$export->export($spreadsheet, 'myFile.xslx', new PHPExcel_Writer_Excel2007());

Writers available:

  • Excel 2007 (.xlsx): PHPExcel_Writer_Excel2007
  • Excel classic (.xls): PHPExcel_Writer_Excel5
  • CSV (.csv): PHPExcel_Writer_CSV