Bridge with openspout/openspout
Refer to the official documentation on GitHub.
This bridge provides ways to read/write from/to CSV/ODS/XLSX files.
Item reader
The FlatFileReader is a reader that will read from CSV/ODS/XLSX file and return each line as an array.
<?php
declare(strict_types=1);
use Yokai\Batch\Bridge\OpenSpout\Reader\FlatFileReader;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Read .xlsx file
// Every sheet will be read
// All lines will be read as simple array
new FlatFileReader(
filePath: new StaticValueParameterAccessor('/path/to/file.xlsx'),
);
<?php
declare(strict_types=1);
use OpenSpout\Reader\CSV\Options as CSVOptions;
use Yokai\Batch\Bridge\OpenSpout\Reader\FlatFileReader;
use Yokai\Batch\Bridge\OpenSpout\Reader\HeaderStrategy;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Read .csv file
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
// Each lines will be read as simple array
$options = new CSVOptions();
$options->FIELD_DELIMITER = ';';
$options->FIELD_ENCLOSURE = '|';
new FlatFileReader(
filePath: new StaticValueParameterAccessor('/path/to/file.csv'),
options: $options,
headerStrategy: HeaderStrategy::none(),
);
<?php
declare(strict_types=1);
use Yokai\Batch\Bridge\OpenSpout\Reader\FlatFileReader;
use Yokai\Batch\Bridge\OpenSpout\Reader\HeaderStrategy;
use Yokai\Batch\Bridge\OpenSpout\Reader\SheetFilter;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Read .ods file
// Only sheet named "Sheet name to read" will be read
// Each item will be an array_combine of first line as key and line as values
new FlatFileReader(
filePath: new StaticValueParameterAccessor('/path/to/file.ods'),
sheetFilter: SheetFilter::nameIs('Sheet name to read'),
headerStrategy: HeaderStrategy::combine(),
);
See also
Item writer
The FlatFileWriter is a writer that will write to CSV/ODS/XLSX file and each item will written its own line.
<?php
declare(strict_types=1);
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Write items to .xlsx file
// That file will not contain a header line
new FlatFileWriter(
filePath: new StaticValueParameterAccessor('/path/to/file.xlsx'),
);
<?php
declare(strict_types=1);
use OpenSpout\Writer\CSV\Options as CSVOptions;
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Write items to .csv file
// That file will not contain a header line
// The CSV delimiter and enclosure has been changed from default (respectively ',' & '"')
$options = new CSVOptions();
$options->FIELD_DELIMITER = ';';
$options->FIELD_ENCLOSURE = '|';
new FlatFileWriter(
filePath: new StaticValueParameterAccessor('/path/to/file.csv'),
options: $options,
);
<?php
declare(strict_types=1);
use OpenSpout\Common\Entity\Style\Style;
use OpenSpout\Writer\ODS\Options as ODSOptions;
use Yokai\Batch\Bridge\OpenSpout\Writer\FlatFileWriter;
use Yokai\Batch\Job\Parameters\StaticValueParameterAccessor;
// Write items to .ods file
// That file will contain a header line with: static | header | keys
// Change the sheet name data will be written
// Change the default style of each cell
$options = new ODSOptions();
$options->DEFAULT_ROW_STYLE = (new Style())->setFontBold();
new FlatFileWriter(
filePath: new StaticValueParameterAccessor('/path/to/file.ods'),
options: $options,
defaultSheet: 'The sheet name',
headers: ['static', 'header', 'keys'],
);
See also