How can I set encoding for export csv with Laravel Excel in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering CSV Encoding with Laravel Excel: A Developer's Guide
When you are exporting data from your Laravel application using a powerful package like Laravel Excel, ensuring the resulting CSV file has the correct character encoding is crucial. A seemingly simple text export can become a source of headaches when dealing with international characters, special symbols, or specific regional standards. Many developers run into the issue you described: attempting to force an encoding via configuration or manual conversion doesn't seem to stick, as the output format often overrides these settings.
As senior developers, we understand that file I/O and character encoding are complex topics rooted in how PHP interacts with the underlying operating system. Let’s dive deep into why this happens and establish the most robust solution for handling CSV encoding within the Laravel Excel ecosystem.
Understanding the Encoding Conflict
The core issue lies in the separation between the data content (which is UTF-8 by modern standards) and the file writing process (which relies on PHP's stream functions). When exporting, if the environment defaults to a regional encoding (like SJIS or specific Windows encodings), it can implicitly re-encode your UTF-8 data during the write operation, leading to corruption or incorrect character representation in the final .csv file.
Your attempts—modifying excel.php or using mb_convert_encoding—are excellent starting points, but they address the data preparation rather than the output mechanism. The solution often requires intervening directly within the export class to explicitly define how the data is written to the stream.
The Robust Solution: Enforcing UTF-8 Output
The most reliable method for ensuring correct CSV encoding in a Laravel Excel context is to guarantee that your input data is UTF-8, and then ensure the exporter writes this clean text directly without unnecessary conversion layers.
Step 1: Ensure Data Integrity (UTF-8 Source)
Always ensure all data stored in your Eloquent models or collections is stored and handled as UTF-8. This is the universal standard for web applications and file exports.
// Example ensuring a string is UTF-8 compliant before export
$data = $this->someModel->name;
// If you suspect non-UTF8 input, normalize it here:
$utf8Data = mb_convert_encoding($data, 'UTF-8', 'ISO-8859-1');
Step 2: Customizing the Export Class for Control
Since the default behavior seems to ignore configuration settings in favor of system defaults, we need to take manual control within our specific export class. We can achieve this by overriding or explicitly defining the CSV writing process inside your Export class.
When using Laravel Excel, you interact with the writer object. By leveraging the underlying functionality provided by the package, we can ensure the proper delimiters and encodings are respected. While there isn't a single global setting in excel.php that forces all file outputs to use UTF-8 regardless of system settings, controlling the data stream is paramount.
Here is how you might structure your export class to explicitly manage the writing process:
namespace App\Exports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithCsvSettings;
class MyCustomCsvExport implements FromCollection, WithCsvSettings
{
public function collection()
{
// Ensure the collection data is clean UTF-8 strings
return collect([
['Name', 'Email'],
['Alice', 'alice@example.com'],
['Bob', 'bob@example.com'],
]);
}
public function csvSettings(): array
{
// Explicitly setting the delimiter and ensuring UTF-8 context is often handled here
return [
'delimiter' => ',',
'line_ending' => "\r\n", // Standard Windows line ending for compatibility
'encoding' => 'UTF-8', // While Excel handles this, explicitly stating it helps documentation and consistency.
];
}
}
By defining the settings within the export class itself, you make the encoding requirement explicit for that specific file generation, which is far more reliable than relying on global configuration flags when dealing with complex I/O operations. This approach aligns perfectly with the modular design principles championed by frameworks like those found at https://laravelcompany.com.
Conclusion
Setting CSV encoding correctly in Laravel Excel is less about finding a single magic switch and more about controlling the data pipeline from source to destination. Avoid relying solely on global configuration settings for file output. Instead, focus on ensuring your input data is consistently UTF-8 (Step 1) and then explicitly define the required settings within your custom export class (Step 2). This developer-centric approach provides the control necessary to generate perfectly encoded CSV files every time.