Installation:
composer require boenrobot/big-xlsx-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Boenrobot\BigXlsxBundle\BoenrobotBigXlsxBundle::class => ['all' => true],
First Use Case: Generate a simple XLSX file with one sheet:
use Boenrobot\BigXlsxBundle\Service\BigXlsxService;
$service = $this->get('boenrobot_big_xlsx.service');
$data = [["id", "name"], [1, "Test"]];
$service->addSheet(0, "Sheet 1", $data);
$file = $service->getFile();
return new BinaryFileResponse($file, 200, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
BigXlsxService is the core interface for sheet management.Basic Usage and Adding a Custom Sheet sections.getPHPExcel() exposes the underlying library for advanced use.Data Preparation:
$data = [["id", "name"], [1, "Alice"], [2, "Bob"]];
Sheet Management:
addSheet($index, $name, $data) for pre-defined sheets.getPHPExcel() to create sheets programmatically:
$phpExcel = $service->getPHPExcel();
$phpExcel->createSheet()->setCellValue('A1', 'Custom Data');
Memory Efficiency:
getFile() to stream the file directly to the client:
$response = new StreamedResponse(function () use ($file) {
echo $file->getContent();
});
$response->setCallback(null);
$response->send();
Integration with Controllers:
public function exportAction() {
$service = $this->get('boenrobot_big_xlsx.service');
$data = $this->fetchLargeDataset(); // Assume this returns an array
$service->addSheet(0, "Data Export", $data);
return $this->file($service->getFile(), 'export.xlsx');
}
Styling and Formatting:
getPHPExcel() to apply styles:
$phpExcel = $service->getPHPExcel();
$sheet = $phpExcel->getActiveSheet();
$sheet->getStyle('A1')->getFont()->setBold(true);
Multi-Sheet Merging: Combine multiple datasets into separate sheets:
$service->addSheet(0, "Sheet 1", $data1);
$service->addSheet(1, "Sheet 2", $data2);
Conditional Logic: Dynamically generate sheets based on user input or data analysis:
if ($userRequestedSheet) {
$service->addSheet(2, "Custom Sheet", $filteredData);
}
Template Inheritance:
Use getPHPExcel() to clone a template sheet and populate it with data:
$template = $this->loadTemplateExcel();
$phpExcel = $service->getPHPExcel();
$phpExcel->createSheet()->setCellValue('A1', 'Dynamic Data');
Memory Leaks:
Sheet Index Conflicts:
getPHPExcel()->createSheet() for dynamic sheets.PhpSpreadsheet Deprecations:
phpexcel (legacy), which may conflict with newer phpspreadsheet versions.phpexcel to a stable version in composer.json:
"require": {
"phpexcel/phpexcel": "1.8.*"
}
File Handling:
getFile() returns a temporary file that may not persist after script execution.$file = $service->getFile();
file_put_contents('export.xlsx', $file->getContent());
Encoding Issues:
é, ü) may render incorrectly.$writer = \PhpOffice\PhpExcel\IOFactory::createWriter($phpExcel, 'Excel2007');
$writer->setPreCalculateFormulas(false);
$writer->save('export.xlsx');
Check Sheet Count:
$phpExcel = $service->getPHPExcel();
echo $phpExcel->getSheetCount(); // Should match expected count
Inspect Data Structure:
var_dump($data); // Should be [ [col1, col2], [val1, val2], ... ]
Log Warnings:
\PhpOffice\PhpExcel\Settings::setLogLevel(\PhpOffice\PhpExcel\Logger::LOG_DEBUG);
Custom Writers:
BigXlsxService.Event Listeners:
$service->getPHPExcel()->registerCallback(function($sheet) {
$sheet->getStyle('A1:Z1')->applyFromArray([
'font' => ['bold' => true]
]);
});
Dependency Injection:
BigXlsxService to customize behavior (e.g., add encryption):
# config/services.yaml
Boenrobot\BigXlsxBundle\Service\BigXlsxService:
arguments:
$customOption: '%kernel.project_dir%/config/xlsx_template.xlsx'
Performance Optimization:
$data = [];
foreach ($largeDataset as $item) {
$data[] = [$item->id, $item->name];
if (count($data) >= 1000) {
$service->addSheet($sheetIndex++, "Sheet_" . $sheetIndex, $data);
$data = [];
}
}
How can I help you explore Laravel packages today?