Backend Development 5 min read

Using Laravel Excel to Import and Export Excel/CSV Files in Laravel

This tutorial explains how to integrate the Laravel‑Excel package into a Laravel 5 project, covering Composer installation, service provider and alias configuration, publishing the configuration file, creating a controller with export and import methods, and handling CSV, XLS, and XLSX files both for download and server storage.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using Laravel Excel to Import and Export Excel/CSV Files in Laravel

Laravel Excel integrates the PHPOffice PHPExcel library into Laravel, allowing expressive code for Excel and CSV import/export. The package is available at https://github.com/Maatwebsite/Laravel-Excel .

Installation & Configuration

Run the Composer command:

composer require maatwebsite/excel

After installation, add the service provider to the providers array in config/app.php :

Maatwebsite\Excel\ExcelServiceProvider::class,

and register the facade in the aliases array:

'Excel' => Maatwebsite\Excel\Facades\Excel::class,

Publish the default configuration file with:

php artisan vendor:publish

This creates config/excel.php where you can adjust settings.

Exporting an Excel File

Create a plain controller:

php artisan make:controller ExcelController --plain

Define a route in routes.php :

Route::get('excel/export', 'ExcelController@export');
Route::get('excel/import', 'ExcelController@import');

Implement the export method:

<?php
namespace App\Http\Controllers;
use Excel;
class ExcelController extends Controller {
    // Export Excel file
    public function export() {
        $cellData = [
            ['学号','姓名','成绩'],
            ['10001','AAAAA','99'],
            ['10002','BBBBB','92'],
            ['10003','CCCCC','95'],
            ['10004','DDDDD','89'],
            ['10005','EEEEE','96'],
        ];
        Excel::create('学生成绩', function($excel) use ($cellData) {
            $excel->sheet('score', function($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}

Visiting http://your-domain/excel/export downloads 学生成绩.xls . Change the format argument to csv or xlsx to export those types.

To store the file on the server instead of immediate download:

Excel::create('学生成绩', function($excel) use ($cellData) {
    $excel->sheet('score', function($sheet) use ($cellData) {
        $sheet->rows($cellData);
    });
})->store('xls')->export('xls');

The file is saved under storage/exports . If the filename appears garbled, convert it with:

iconv('UTF-8','GBK','学生成绩');

Importing an Excel File

Use the load method of the Excel facade to read a previously stored file:

// Excel import method
public function import() {
    $filePath = 'storage/exports/' . iconv('UTF-8','GBK','学生成绩') . '.xls';
    Excel::load($filePath, function($reader) {
        $data = $reader->all();
        dd($data);
    });
}

The load method uses the project root as the base path, so the same charset conversion is required for Chinese filenames.

backendphpCSVexcelLaravelImportExportLaravel-Excel
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.