PHP code example of mradang / laravel-excel

1. Go to this page and download the library: Download mradang/laravel-excel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mradang / laravel-excel example snippets


ExcelService::read(
    $pathname, // excel 文件绝对路径
    1, // 字段名行号
    [
        // 字段定义
        'id' => '编号',
        'name' => '姓名',
        'age' => '年龄',
    ],
    2, // 数据开始的行号
    function (array $cells) {
        // 行数据处理函数,每行调用一次
        /**
         * $cells = [
         *   'id' => 1,
         *   'name' => '张三',
         *   'age' => 21,
         * ];
         */
    },
);

$pathname = ExcelService::write(
    '首行通栏标题',
    [
        // 字段定义
        'id' => '编号',
        'name' => '姓名',
        'age' => '年龄',
    ],
    ['id', 'age'], // 数字列
    [
        // 数据
        ['id' => 4, 'name' => '赵六', 'age' => 24],
        ['id' => 5, 'name' => '孙七', 'age' => 25],
        ['id' => 6, 'name' => '周八', 'age' => 26],
    ],
    function (int $index, $row) {
        // 行数据处理函数,每行调用一次,返回字段值数组
        return array_values($row);
    },
    2, // 冻结字段列
);