Installation:
composer require arodiss/xls-bundle
Ensure Symfony\Process and phpoffice/phpexcel are installed (handled automatically via Composer).
Enable the Bundle (if not auto-discovered):
Add to config/bundles.php:
Arodiss\XlsBundle\ArodissXlsBundle::class => ['all' => true],
First Use Case: Read an XLS File Inject the reader service and read a file:
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExcelController
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function readExcel()
{
$reader = $this->container->get('arodiss.xls.reader');
$content = $reader->readAll('/path/to/file.xls');
// $content is an array of rows (e.g., [["Header1", "Header2"], ["Data1", "Data2"]])
}
}
Reading Excel Files
readAll() for simplicity:
$reader->readAll('/path/to/file.xls');
$iterator = $reader->getReadIterator('/path/to/file.xls');
foreach ($iterator as $row) {
// Process $row (e.g., [["Col1", "Col2"], ["Value1", "Value2"]])
}
xlrd/openpyxl):
$pythonReader = $this->container->get('arodiss.xls.reader.python');
$iterator = $pythonReader->getReadIterator('/path/to/large_file.xls');
Writing Excel Files
$writer = $this->container->get('arodiss.xls.writer.buffered');
$writer->create('/path/to/output.xlsx', ['Name', 'Email']); // Header row
foreach ($users as $user) {
$writer->appendRow('/path/to/output.xlsx', [$user->name, $user->email]);
}
$writer->flush(); // Write all data at once
arodiss.xls.writer instead of buffered for non-buffered writes (not recommended for performance).Exporting from Controllers Generate and return an XLSX file:
$builder = $this->container->get('arodiss.xls.builder');
$filePath = $builder->buildXlsFromArray([
['Header1', 'Header2'],
['Data1', 'Data2']
]);
$response = new Response(file_get_contents($filePath));
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Disposition', 'attachment;filename=export.xlsx');
return $response;
Integration with Forms/Validation
$file = $request->files->get('excel_file');
if ($file && $file->getClientOriginalExtension() === 'xls') {
$reader = $this->container->get('arodiss.xls.reader');
$data = $reader->readAll($file->getPathname());
// Validate $data (e.g., check required columns)
}
Memory Limits with readAll()
getReadIterator()) for large files.Python Reader Dependencies
xlrd (for .xls) and openpyxl (for .xlsx).pip:
pip install xlrd openpyxl
Write Performance
arodiss.xls.writer) are slow for large datasets.buffered writer to minimize disk I/O.File Format Quirks
.xlsx formats (e.g., .xls, .csv), but no guarantees..xlsx for consistency. Convert files beforehand if needed.Temporary File Handling
buildXlsFromArray() creates temp files; ensure they’re cleaned up.unlink() or Symfony’s Filesystem component to delete temp files post-download.Check PHPExcel Errors
PHPExcel_Settings::setLogFile('phpexcel.log');
Validate Python Reader Output
Monitor Buffer Usage
buffered writes, log buffer size to avoid hitting memory limits:
$writer->setMaxBufferSize(1000); // Limit rows in memory
Custom Cell Formatting
PHPExcel styles via the builder:
$builder->getPHPExcelObject()->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
Pre/Post-Processing Hooks
# config/services.yaml
services:
arodiss.xls.reader:
class: App\Service\CustomXlsReader
decorates: 'arodiss.xls.reader'
arguments: ['@arodiss.xls.reader.inner']
Batch Processing
Iterator utilities for complex workflows:
use Symfony\Component\Iterator\FilterIterator;
$filteredIterator = new FilterIterator($reader->getReadIterator('/file.xls'), function($row) {
return $row[0] === 'TargetValue';
});
How can I help you explore Laravel packages today?