PHP code example of lekoala / spread-compat

1. Go to this page and download the library: Download lekoala/spread-compat 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/ */

    

lekoala / spread-compat example snippets


// Override the default when you specifically need another adapter
SpreadCompat::$preferredXlsxAdapter = SpreadCompat::XLSWRITER;

$data = iterator_to_array(SpreadCompat::read('myfile.csv'));

// or
foreach(SpreadCompat::read('myfile.xlsx') as $row) {
    // Do something
}

// or even
foreach(SpreadCompat::read('myfile.ods') as $row) {
    // Do something
}

SpreadCompat::output($data, 'myfile.csv');
exit();

SpreadCompat::output(
    $data,
    'contacts',
    extension: 'xlsx',
    columnFormats: ['E' => '@', 'F' => '@', 'G' => '0.00']
);

SpreadCompat::output($data, 'contacts', extension: 'xlsx', forceText: true);

// Rows are read lazily
foreach (SpreadCompat::read('large.xlsx') as $row) {
    // Do something
}

// Rows are written lazily
SpreadCompat::write($rowsGenerator, 'large.csv');

$stream = SpreadCompat::writeStream($rows, 'xlsx');

try {
    // PSR-7, storage API, attachment, ...
} finally {
    fclose($stream);
}

$data = iterator_to_array(SpreadCompat::read('myfile.csv', assoc: true));

// or
$data = iterator_to_array(SpreadCompat::read('myfile.csv', ...$opts));

$options = new Options();
$options->separator = ";";
$data = iterator_to_array(SpreadCompat::read('myfile.csv', $options));

$csvData = SpreadCompat::readString($csv, adapter: SpreadCompat::BARESHEET);
// or
$options = new Options();
$options->adapter = SpreadCompat::BARESHEET;
$csvData = SpreadCompat::readString($csv, $options);

SpreadCompat::write($data, 'myfile.csv', escapeFormulas: true);