Installation: Add the bundle via Composer (note: requires Symfony2 compatibility):
composer require liuggio/excelbundle
For Laravel, manually register the service in a ServiceProvider (see Implementation Patterns).
First Use Case: Generate a simple Excel file in a Laravel controller:
use Liuggio\ExcelBundle\Services\PHPExcelService;
class ExcelController extends Controller {
public function export() {
$excel = app()->make(PHPExcelService::class);
$phpExcelObject = $excel->createPHPExcelObject();
$phpExcelObject->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello, Laravel!')
->setCellValue('B1', 'ExcelBundle');
$writer = $excel->createWriter($phpExcelObject, 'Excel5');
$response = $excel->createStreamedResponse($writer);
return $response->setContentDisposition(
'attachment',
'export.xlsx'
);
}
}
Where to Look First:
Liuggio\ExcelBundle\Services\PHPExcelService (core methods: createPHPExcelObject(), createWriter(), createStreamedResponse()).$excel = app()->make(PHPExcelService::class);
$phpExcel = $excel->createPHPExcelObject();
$sheet = $phpExcel->getActiveSheet();
$users = User::all();
$sheet->fromArray($users->toArray(), null, 'A1', true, false);
return $excel->createStreamedResponse(
$excel->createWriter($phpExcel, 'Excel2007')
)->setContentDisposition('attachment', 'users.xlsx');
$headers = ['ID', 'Name', 'Email'];
$sheet->fromArray([$headers], null, 'A1');
$sheet->fromArray($users->pluck(['id', 'name', 'email'])->toArray(), null, 'A2');
$excel = app()->make(PHPExcelService::class);
$phpExcel = $excel->createPHPExcelObject('path/to/file.xlsx');
$sheet = $phpExcel->getActiveSheet();
$data = $sheet->toArray(null, true, true, true); // Associative arrays
foreach ($data as $row) {
User::create($row);
}
Excel2007 writer for chunked processing:
$writer = $excel->createWriter($phpExcel, 'Excel2007');
$writer->setUseWriteCache(false); // Disable cache for large files
$response = $excel->createStreamedResponse($writer);
$drawing = $excel->createPHPExcelWorksheetDrawing();
$drawing->setPath(public_path('logo.png'))
->setCoordinates('A1')
->setWorksheet($sheet);
$style = $sheet->getStyle('A1');
$style->getFont()->setBold(true);
$style->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
Service Registration:
Create a ExcelServiceProvider:
use Liuggio\ExcelBundle\Services\PHPExcelService;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ExcelServiceProvider extends ServiceProvider {
public function register() {
$this->app->singleton(PHPExcelService::class, function ($app) {
$service = new PHPExcelService();
// Mock Symfony's StreamedResponse if needed
$service->setStreamedResponseFactory(function ($writer) {
return new StreamedResponse(function () use ($writer) {
$writer->save('php://output');
});
});
return $service;
});
}
}
Dependency Conflicts:
spatie/laravel-excel or maatwebsite/excel, avoid mixing with this bundle (conflicting PHPOffice versions).PHPExcel or use PhpSpreadsheet via maatwebsite/excel.For Large Datasets:
Excel2007 format (better compression).$writer->setUseWriteCache(false).Memory Management:
unset($phpExcel);
gc_collect_cycles();
Custom Templates: Load a template and merge data:
$template = $excel->createPHPExcelObject('template.xlsx');
$sheet = $template->getActiveSheet();
$sheet->setCellValue('B5', 'Dynamic Data');
Dynamic Sheets:
$phpExcel = $excel->createPHPExcelObject();
foreach ($groups as $group) {
$sheet = $phpExcel->createSheet();
$sheet->setTitle($group->name);
$sheet->fromArray($group->users->toArray());
}
$phpExcel->removeSheetByIndex(0); // Remove default sheet
Charts:
$sheet->getChartCollection()->createChart('chart1', 'pie');
$series = $sheet->getChartCollection()->getChartByName('chart1')->createSeries();
$series->addDataRange('B2:B10');
Symfony Dependency Hell:
Class 'Symfony\Component\HttpFoundation\StreamedResponse' not found.StreamedResponse (see Integration Tips).PHPExcel Deprecation:
Class 'PHPExcel' not found or PHPExcel_Reader_Exception.PhpSpreadsheet via maatwebsite/excel (recommended for Laravel). If stuck with this bundle:
composer require phpoffice/phpexcel ~1.8.1
Memory Limits:
Allowed memory size exhausted for files >5MB.memory_limit in php.ini.Excel2007 format and stream responses.Encoding Issues:
é instead of é).$phpExcel->getProperties()->setCreator('UTF-8 Creator');
$writer->setPreCalculateFormulas(false);
$writer->setUseDiskCaching(false);
Large File Timeouts:
setOutputEncoding('UTF-8') on the writer.shouldQueue() on the job.Image Paths:
Image not found when adding drawings.$drawing->setPath(public_path('images/logo.png'));
Validate Excel Objects:
if (!$phpExcel instanceof PHPExcel) {
throw new \RuntimeException('Invalid PHPExcel object');
}
Log Writer Errors:
try {
$writer->save('php://output');
} catch (\Exception $e) {
\Log::error('Excel export failed: ' . $e->getMessage());
throw $e;
}
Check File Permissions:
www-data) can read/write to the target directory.Excel5 (.xls):How can I help you explore Laravel packages today?