PHP framework laravel Excel Package Use tutorial Introduction

Source: Internet
Author: User
Tags closure php file php framework composer install

Installation of Excel Plug-ins in Laravel

Introduction of Laravel Excel packages in composer

"Maatwebsite/excel": "1.*"
To edit the app.php file under Laravel/app/config, add the following values in the providers array

' Maatwebsite\excel\excelserviceprovider ',
Find the aliasses array in the same file add the following values

' Excel ' => ' maatwebsite\excel\facades\excel ',
Executes the composer install or composer Update command.

Laravel Excel Configuration

Some of the configuration items for Plug-ins are located under Laravel/vendor/maatwebsite/excel/src/config

config.php > Some settings for Excel and table global
csv.php > settings for exporting CSV files to import
Export.pho > Some settings to print out the contents of a file
import.php > settings for importing Excel files

Simple use of Laravel Excel

We'll be able to use the Excel plugin after all the previous preparations have been done.

Export Excel

<?php
$rows = Array (array (' ID ' => 1, ' name ' => ' Marlon '));

Excel::create ($name, function ($excel) use ($rows) {
$excel->sheet (' Registration of the day ', function ($sheet) use ($rows) {
$sheet->fromarray ($rows);
});
})->store (' xls ', Storage_path (' Excel '));

Since it is not possible to get the variables outside the closure in the PHP closure, you need to introduce $rows with use, and the parameters that are passed in the store of the last chained call are the format of Excel and the location to save to the server, which is the absolute path.

In this place store () method for storage, the corresponding can also use the download () method to download directly, as for the export method The author has not understood what is the use of

Import Excel

<?php
Excel::load (Input::file (' Excel '), function ($reader) {
Get the first few tables of Excel
$reader = $reader->getsheet (0);
Get data from a table
$results = $reader->toarray ();
Here, $results is already in Excel data, can be here to operate on him, warehousing or other ....
});

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.