PHP code example of moxuandi / yii2-phpspreadsheet

1. Go to this page and download the library: Download moxuandi/yii2-phpspreadsheet 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/ */

    

moxuandi / yii2-phpspreadsheet example snippets


$data = [
    ['id' => 1, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 211325659536289526],
    ['id' => 2, 'name' => '李四', 'sex' => '男'],
    ['id' => 5, 'name' => '王二', 'sex' => '女'],
    ['id' => 8, 'name' => '麻子', 'sex' => '女'],
];
ExportData::widget([
    'data' => $data,  // 必须
    'headers' => [],  // 第一行的标题栏
    'sheetTitle' => 'sheet',  // 工作表的标题
    'setFirstTitle' => false,  // 是否在第一行设置标题行
    'asAttachment' => false,  // 是否下载导出结果
    'fileName' => 'word.xls',  // 导出的 Excel 文件名
    'savePath' => 'uploads/excels/',  // 保存到服务器的路径
    'properties' => [],  // Excel 文件的属性列表
]);

// 在一个 Excel 中导出多个工作表:
$data = [
    'sheet1' => [
        ['id' => 1, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 2, 'name' => '李四', 'sex' => '男'],
        ['id' => 5, 'name' => '王二', 'sex' => '女'],
        ['id' => 8, 'name' => '麻子', 'sex' => '女'],
    ],
    'sheet2' => [
        ['id' => 11, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 12, 'name' => '李四', 'sex' => '男'],
        ['id' => 15, 'name' => '王二', 'sex' => '女'],
        ['id' => 18, 'name' => '麻子', 'sex' => '女'],
    ],
    'sheet3' => [
        ['id' => 21, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 22, 'name' => '李四', 'sex' => '男'],
        ['id' => 25, 'name' => '王二', 'sex' => '女'],
        ['id' => 28, 'name' => '麻子', 'sex' => '女'],
    ],
];
ExportData::widget([
    'data' => $data,  // 必须
    'isMultipleSheet' => true,  // 必须
    'sheetTitle' => ['sheet1' => 'sheet1', 'sheet2' => 'sheet2'],  // 工作表的标题
]);