1. Go to this page and download the library: Download lekoala/baresheet 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 / baresheet example snippets
use LeKoala\Baresheet\Baresheet;
use LeKoala\Baresheet\Options;
// Auto-detect format from extension
$rows = Baresheet::read('data.xlsx', new Options(assoc: true));
foreach ($rows as $row) {
echo $row['email'];
}
// Write — format from extension
Baresheet::write($data, 'output.csv', new Options(bom: false));
Baresheet::write($data, 'output.xlsx', new Options(meta: ['creator' => 'My App']));
Baresheet::write($data, 'output.ods');
// Write to string
$csv = Baresheet::writeString($data, 'csv');
$xlsx = Baresheet::writeString($data, 'xlsx');
$ods = Baresheet::writeString($data, 'ods');
// Write to PHP resource (for PSR-7 or Laravel Responses)
$stream = Baresheet::writeStream($data, 'xlsx');
// Stream as download (sends HTTP headers)
Baresheet::output($data, 'report.xlsx');
use LeKoala\Baresheet\Options;
use LeKoala\Baresheet\CsvReader;
use LeKoala\Baresheet\CsvWriter;
use LeKoala\Baresheet\XlsxReader;
use LeKoala\Baresheet\XlsxWriter;
// CSV - Manual pattern
$reader = new CsvReader();
$reader->assoc = true;
$rows = $reader->readFile('data.csv');
// CSV - Options pattern
$writer = new CsvWriter(new Options(
escapeFormulas: true,
));
$writer->writeFile($data, 'safe-export.csv');
// XLSX - Manual pattern
$reader = new XlsxReader();
$reader->sheet = 'Data';
$rows = $reader->readFile('report.xlsx');
// XLSX - Options pattern
$writer = new XlsxWriter(new Options(
meta: ['creator' => 'My App'],
));
$writer->writeFile($data, 'report.xlsx');
$reader = new CsvReader();
$reader->assoc = true;
$reader->separator = ";";
// Or directly in the constructor
$reader = new CsvReader(new Options(assoc: true, separator: ";"));
use LeKoala\Baresheet\Options;
$opts = new Options(
assoc: true,
separator: 'auto',
meta: ['creator' => 'My App']
);
$rows = Baresheet::read('data.csv', $opts);
// Throws RuntimeException if 'email' or 'price' columns are missing
$rows = Baresheet::read('products.csv', new Options(
assoc: true,
// Select specific columns (assoc mode returns named array)
$rows = Baresheet::read('data.csv', new Options(
assoc: true,
columns: ['email', 'name'] // Only these columns, in this order
));
foreach ($rows as $row) {
// $row contains only ['email' => '...', 'name' => '...']
}
// File has: name, email, age (in that order)
// Output: age first, then name
$rows = Baresheet::read('data.csv', new Options(
assoc: true,
columns: ['age', 'name']
));
$writer = new XlsxWriter();
$writer->stream = false;
$writer->output($data, 'report.xlsx');
// or via Options
Baresheet::output($data, 'report.xlsx', new Options(stream: false));
use LeKoala\Baresheet\XlsxWriter;
use Symfony\Component\HttpFoundation\StreamedResponse;
$writer = new XlsxWriter();
$stream = $writer->writeStream($data);
return new StreamedResponse(function () use ($stream) {
fpassthru($stream);
fclose($stream);
}, 200, [
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => 'attachment; filename="report.xlsx"',
]);