Installation Add the bundle via Composer in your Laravel project (Symfony-compatible):
composer require agence-gw/excelbundle
Register the bundle in config/app.php under providers (Laravel 5.5+ uses auto-discovery, but manual registration may be needed for older versions):
AgenceGw\ExcelBundle\AgenceGwExcelBundle::class,
Basic Usage
Inject the ExcelManager service into your controller/service:
use AgenceGw\ExcelBundle\Manager\ExcelManager;
public function __construct(ExcelManager $excelManager) {
$this->excelManager = $excelManager;
}
First Use Case: Export to XLSX
public function exportToExcel() {
$excel = $this->excelManager->createExcel('my_file.xlsx');
$sheet = $excel->getSheet(0);
$sheet->setCellValue('A1', 'Hello, Excel!');
$sheet->setCellValue('B1', 'Laravel + PHPExcel');
return $this->excelManager->download($excel, 'my_file.xlsx');
}
public function importExcel(Request $request) {
$file = $request->file('excel_file');
$excel = $this->excelManager->readExcel($file->getPathname());
$sheet = $excel->getSheet(0);
$data = [];
foreach ($sheet->getRowIterator() as $row) {
$data[] = $row->getValues();
}
return response()->json($data);
}
public function generateMultiSheetReport() {
$excel = $this->excelManager->createExcel('report.xlsx');
// Add first sheet
$sheet1 = $excel->createSheet();
$sheet1->setTitle('Sales Data');
$sheet1->setCellValue('A1', 'Product');
$sheet1->setCellValue('B1', 'Revenue');
// Add second sheet
$sheet2 = $excel->createSheet();
$sheet2->setTitle('Expenses');
$sheet2->setCellValue('A1', 'Category');
$sheet2->setCellValue('B1', 'Amount');
return $this->excelManager->download($excel, 'report.xlsx');
}
public function styledExport() {
$excel = $this->excelManager->createExcel('styled.xlsx');
$sheet = $excel->getSheet(0);
// Header style
$headerStyle = $excel->getStyleFactory()->createStyle();
$headerStyle->getFont()->setBold(true);
$headerStyle->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Name');
$sheet->getStyle('A1:B1')->applyFromArray($headerStyle->toArray());
return $this->excelManager->download($excel, 'styled.xlsx');
}
public function exportUserData() {
$users = User::all();
$excel = $this->excelManager->createExcel('users.xlsx');
$sheet = $excel->getSheet(0);
$sheet->setCellValue('A1', 'ID');
$sheet->setCellValue('B1', 'Email');
$sheet->setCellValue('C1', 'Created At');
$row = 2;
foreach ($users as $user) {
$sheet->setCellValue('A' . $row, $user->id);
$sheet->setCellValue('B' . $row, $user->email);
$sheet->setCellValue('C' . $row, $user->created_at->format('Y-m-d'));
$row++;
}
return $this->excelManager->download($excel, 'users.xlsx');
}
Memory Limits
ini_set('memory_limit', '512M'); // Temporarily increase limit
File Path Handling
$filePath = storage_path('app/uploads/report.xlsx');
$excel = $this->excelManager->readExcel($filePath);
Sheet Indexing
// Throws exception if no sheet exists at index 5
$sheet = $excel->getSheet(5);
Encoding Issues
é, ñ) may corrupt Excel files. Use UTF-8 encoding explicitly:
$excel->getProperties()->setCreator('UTF-8 Creator');
PDF/Odt Support Limitations
.xlsx/.xls. PDF/ODT support is limited and may require additional libraries (e.g., mikehaertl/phpwkhtmltopdf for PDF).Reuse Styles Define styles once and reuse them:
$boldStyle = $excel->getStyleFactory()->createStyle()
->getFont()
->setBold(true);
$sheet->getStyle('A1:A10')->applyFromArray($boldStyle->toArray());
Merge Cells
$sheet->mergeCells('A1:B1');
$sheet->setCellValue('A1', 'Merged Header');
Auto-Size Columns
$sheet->getColumnDimension('A')->setAutoSize(true);
Protect Sheets
$sheet->getProtection()->setSheet(true);
$sheet->getProtection()->setPassword('secret');
Leverage Events Use events for pre/post-processing:
$excel->addListener(new MyExcelEventListener());
Validation Validate Excel files before processing:
if (!$excel->isValid()) {
throw new \RuntimeException('Invalid Excel file');
}
Laravel Artisan Commands Create custom commands for bulk exports:
use AgenceGw\ExcelBundle\Manager\ExcelManager;
class ExportUsersCommand extends Command {
protected $excelManager;
public function __construct(ExcelManager $excelManager) {
$this->excelManager = $excelManager;
parent::__construct();
}
public function handle() {
// Export logic here
}
}
Testing
Mock ExcelManager in tests:
$this->mock(ExcelManager::class)->shouldReceive('download')->once();
How can I help you explore Laravel packages today?