Laravel 5 uses Laravel Excel to import and export Excel/CSV files.

Source: Internet
Author: User

Laravel 5 uses Laravel Excel to import and export Excel/CSV files.

1. Introduction

This article mainly introduces Laravel 5's use of Laravel Excel to import and export Excel/CSV files. I will not talk about it here. Let's take a look at the details.

Laravel Excel integrates PHPExcel in the PHPOffice suite in Laravel 5 to facilitate the import and export of Excel/CSV files with elegant and expressive code.

The GitHub address of this project is https://github.com/maatwebsite/laravel-excel.

Local: http://xiazai.jb51.net/201710/yuanma/Laravel-Excel-2.1 (jb51.net).rar

2. installation and configuration

Use Composer to install Dependencies

In this article, we will use Laravel Excel in Laravel to import and export Excel files.

First, go to the root directory of the Laravel project and use Composer to install dependencies:

composer require maatwebsite/excel=~2.0 

Setup after installation

Register the service provider in config/app. php to the providers array:

Maatwebsite\Excel\ExcelServiceProvider::class,

Similarly, register the facade to the aliases array in config/app. php:

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

If you want to customize Laravel Excel, run the following Artisan command:

php artisan vendor:publish

After successful execution, a configuration file excel. php is generated in the config directory.

3. Export an Excel file

To demonstrate Laravel Excel functions, we have created a clean controller ExcelController. php for this test:

php artisan make:controller ExcelController --plain

Then define related routes in routes. php:

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

Next, we first define the export method in ExcelController. php to implement the export function:

<? Phpnamespace App \ Http \ Controllers; use Illuminate \ Http \ Request; use App \ Http \ Requests; use App \ Http \ Controllers \ Controller; use Excel; class ExcelController extends Controller {// public function export () {$ cellData = [['student ID ', 'name', 'score'], ['123 ', 'aaaaa', '99'], ['000000', 'bbbbbb', '92'], ['000000', 'ccccccccc', '95'], ['123', 'ddddd ', '89'], ['123', 'eeeee ', '96'],]; Excel: create ('student score ', function ($ excel) use ($ cellData) {$ excel-> sheet ('score ', function ($ sheet) use ($ cellData) {$ sheet-> rows ($ cellData) ;})-> export ('xls ');}}


You can also directly import data from arrays.

$sheet->fromArray($anyArray);

To export a csv or xlsx file, you only need to change the parameters in the export method to csv or xlsx.

If you want to save the Excel file to the server, you can use the store method:

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

The file is saved to the storage/exports directory by default. If the file name contains Chinese garbled characters, modify the above Code file name as follows:

Iconv ('utf-8', 'gbk', 'student score ')

4. Import an Excel file

We can import the Excel file saved on the server. The import is very simple. Just use the load method on the Excel facade:

// Excel File import function By Laravel public function import () {$ filePath = 'Storage/exports /'. iconv ('utf-8', 'gbk', 'student region ').'.xls '; Excel: load ($ filePath, function ($ reader) {$ data = $ reader-> all (); dd ($ data );});}

The load method uses the project root path as the root directory. Similarly, we transcode the Chinese title. Otherwise, the system will prompt that the file does not exist.

Access the http://laravel.app in a browser: 8000/excel/import, the page displays as follows:

Use Laravel Excel to import files

Of course, Laravel Excel has many other features, such as exporting the Blade view to Excel or CSV, and more fine-grained control of import/export, For details, refer to its official documentation: http://www.maatwebsite.nl/laravel-excel/docs

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.