Laravel-admin Customize export Excel features and export pictures

Source: Internet
Author: User
Tags export class



https://www.jianshu.com/p/91975f66427d



Recently used Laravel-admin to do a small project, which used the Excel export function.



But the laravel-admin comes with an export feature without pictures, and the exported data has many redundant fields, not the functionality I need.



So refer to the official document adjustment code, the implementation of custom export Excel file, and with slices;



The steps are as follows:



1. Installing the Laravel-excel Plugin



Refer to using Laravel-excel in laravel5.5


 
composer require maatwebsite/excel:~2.1.0

php artisan vendor:publish

--provider="MaatwebsiteExcelExcelServiceProvider"


2. Custom Export Classes



I am a reference to the Laravel-admin official document established export class: app/admin/extensions/excelexpoter.php;



The export class requires the introduction of the Excel used, and the phpexcel_worksheet_drawing used to import the picture


 Use use MaatwebsiteExcelFacadesExcel;

use PHPExcel_Worksheet_Drawing;
The specific code is as follows:
<? php
    / **
     * Created by PhpStorm.
     * User: yuran
     * Date: 10/13/2018
     * Time: 10:04
     * /
    namespace App \ Admin \ Extensions;
    use Encore \ Admin \ Grid \ Exporters \ AbstractExporter;
    use Maatwebsite \ Excel \ Facades \ Excel;
    use PHPExcel_Worksheet_Drawing;

    class ExcelExpoter extends AbstractExporter
    {
        protected $ head = [];
        protected $ body = [];
        public function setAttr ($ head, $ body) {
            $ this-> head = $ head;
            $ this-> body = $ body;
        }

        public function export ()
        {
            // define file name as date spelled uniqid ()
            $ fileName = date (‘YmdHis‘). ‘-’. uniqid ();

            Excel :: create ($ fileName, function ($ excel) {
                $ excel-> sheet (‘sheet1’, function ($ sheet) {
                    // This logic is to take the fields to be exported from the table data
                    $ head = $ this-> head;
                    $ body = $ this-> body;
                    // init column
                    $ title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L ',' M ',' N ',' O ',' P ',' Q ',
                        'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD ',' AE ',' AF ',' AG ',' AH '];

                    $ rows = collect ([$ head]); // write header
                    $ sheet-> rows ($ rows);
                    collect ($ this-> getData ())-> map (function ($ item, $ k) use ($ body, $ sheet, $ title_array) {
                        foreach ($ body as $ i => $ keyName) {
                            if ($ keyName == ‘url’) {// Judge the picture column, if it is, put the picture
                                $ objDrawing = new PHPExcel_Worksheet_Drawing;
                                $ v = public_path (‘/ upload /‘). array_get ($ item, $ keyName); // Stitch image address
                                $ objDrawing-> setPath ($ v);
                                $ sp = $ title_array [$ i];
                                $ objDrawing-> setCoordinates ($ sp. ($ k + 2));
                                $ sheet-> setHeight ($ k + 2, 65); // Set the height
                                $ sheet-> setWidth (array ($ sp => 12)); // Set the width
                                $ objDrawing-> setHeight (80);
                                $ objDrawing-> setOffsetX (1);
                                $ objDrawing-> setRotation (1);
                                $ objDrawing-> setWorksheet ($ sheet);
                            } else {// Otherwise place text data
                                $ v = array_get ($ item, $ keyName);
                                $ sheet-> cell ($ title_array [$ i]. ($ k + 2), function ($ cell) use ($ v) {
                                    $ cell-> setValue ($ v);
                                });
                            }
                        }
                    });
                });
            })-> export (‘xls’);
        }

    }


3. Call



Use this export class in Model-grid:


$ excel = new ExcelExpoter ();
         $ excel-> setAttr (['id', 'name', 'type', 'QR code', 'uploader'], ['id', 'name', 'type', 'url', 'admin ']);
         $ grid-> exporter ($ excel); 


4. Final effect
Original data





Export Results








Laravel-admin Customize export Excel features and export pictures


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.