A powerful Excel import package for Filament that provides seamless Excel file import functionality with automatic column mapping and memory-efficient processing.
You can install the package via composer:
composer require hayderhatem/filament-excel-import
.xlsx
, .xls
, .csv
and other spreadsheet formatsCanImportRecords
traitReplace Filament's CanImportRecords
trait with CanImportExcelRecords
in your resource:
<?php namespace App\Filament\Resources; use App\Filament\Imports\UserImporter;use App\Models\User;use Filament\Resources\Resource;use HayderHatem\FilamentExcelImport\Actions\FullImportAction;use HayderHatem\FilamentExcelImport\Actions\Concerns\CanImportExcelRecords; class UserResource extends Resource{ protected static ?string $model = User::class; public static function getHeaderActions(): array { return [ FullImportAction::make() ->importer(UserImporter::class), ]; }}
The package supports all of Filament's original import options plus additional Excel-specific features:
FullImportAction::make() ->importer(UserImporter::class) // Standard Filament options ->chunkSize(1000) ->maxRows(10000) ->headerOffset(0) // Row number where headers are located (0-based) ->job(CustomImportJob::class) // Custom job class // Excel-specific options ->activeSheet(0) // Which Excel sheet to import (0-based) ->useStreaming(true) // Force streaming mode ->streamingThreshold(10 * 1024 * 1024) // 10MB threshold for auto-streaming
When importing Excel files with multiple sheets, users can select which sheet to import:
FullImportAction::make() ->importer(UserImporter::class) ->activeSheet(0) // Default to first sheet
The import modal will automatically show a sheet selector dropdown if multiple sheets are detected.
You can add custom form fields to the import modal:
use Filament\Forms\Components\Select;use Filament\Forms\Components\Toggle; FullImportAction::make() ->importer(UserImporter::class) ->additionalFormComponents([ Select::make('department_id') ->label('Default Department') ->options(Department::pluck('name', 'id')) ->required(), Toggle::make('send_welcome_email') ->label('Send Welcome Email') ->default(true), ])
Access additional form data in your importer:
use HayderHatem\FilamentExcelImport\Traits\CanAccessAdditionalFormData; class UserImporter extends Importer{ use CanAccessAdditionalFormData; public function import(array $data, array $map, array $options = []): void { $departmentId = $this->getAdditionalFormValue('department_id'); $sendEmail = $this->getAdditionalFormValue('send_welcome_email', false); // Your import logic... }}
The package automatically detects large files and switches to streaming mode to prevent memory exhaustion:
// Files larger than 10MB (default) will automatically use streamingFullImportAction::make() ->importer(UserImporter::class) ->streamingThreshold(5 * 1024 * 1024) // Custom 5MB threshold
Force streaming mode on or off:
FullImportAction::make() ->importer(UserImporter::class) ->useStreaming(true) // Always use streaming // or ->useStreaming(false) // Never use streaming // or ->useStreaming(null) // Auto-detect (default)
File Size | Standard Import | Streaming Import |
---|---|---|
1MB | ~10MB RAM | ~5MB RAM |
100MB | Memory Error | ~5MB RAM |
1GB | Memory Error | ~5MB RAM |
You can use your own import job class:
use HayderHatem\FilamentExcelImport\Actions\Imports\Jobs\ImportExcel; class CustomImportJob extends ImportExcel{ public function handle(): void { // Custom pre-processing parent::handle(); // Custom post-processing }} // Use in your actionFullImportAction::make() ->importer(UserImporter::class) ->job(CustomImportJob::class)
The package provides user-friendly error messages for common database errors:
// Errors are automatically translated and user-friendly// - "Email field is required" instead of SQL constraint errors// - "Email already exists" instead of unique constraint violations// - "Invalid department reference" instead of foreign key errors
Add custom error message translations:
// resources/lang/en/filament-excel-import.phpreturn [ 'import' => [ 'errors' => [ 'field_required' => 'The :field field is required.', 'field_exists' => 'The :field already exists.', 'invalid_reference' => 'Invalid reference to :table.', ] ]];
.xlsx
, .xls
, .xlsm
, .xltx
, .xltm
.csv
, .txt
The package is a drop-in replacement for Filament's CanImportRecords
trait:
Filament Method | Supported | Notes |
---|---|---|
importer() |
✅ | Fully compatible |
job() |
✅ | Fully compatible |
chunkSize() |
✅ | Fully compatible |
maxRows() |
✅ | Fully compatible |
headerOffset() |
✅ | Fully compatible |
options() |
✅ | Fully compatible |
fileValidationRules() |
✅ | Enhanced for Excel |
Method | Description |
---|---|
useStreaming(bool|null) |
Control streaming mode |
streamingThreshold(int) |
Set auto-streaming threshold |
activeSheet(int) |
Set Excel sheet to import |
additionalFormComponents(array) |
Add custom form fields |
Migrating from Filament's built-in import is simple:
// Beforeuse Filament\Actions\Concerns\CanImportRecords; // Afteruse HayderHatem\FilamentExcelImport\Actions\Concerns\CanImportExcelRecords;
All your existing code will work exactly the same! The package is designed as a drop-in replacement.
Take advantage of new features:
FullImportAction::make() ->importer(UserImporter::class) // Your existing configuration works as-is ->chunkSize(500) ->maxRows(5000) // Add new Excel features ->activeSheet(0) ->useStreaming(true)
The package includes comprehensive tests:
# Run all testscomposer test # Run with coveragecomposer test-coverage # Run package test runnerphp test-runner.php
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email the maintainer instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
Full-stack developer specializing in Laravel and Filament ecosystem development. Passionate about creating developer tools that simplify complex workflows and enhance user experience. Creator of innovative Filament packages that leverage AI technology to make data management more intuitive and accessible.