PHP code example of tiderjian / qs-excel

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

    

tiderjian / qs-excel example snippets


composer 

$options  = [
    'row_count' => 500,    //excel生成的行数
    'headers' => [
        [
            'title' => '读者编号',
        ],
        [
            'title' => '姓名',
        ],
        [
            'title' => '性别',
            'type' => 'list',
            'data_source' => '女,男'
        ],
        [
            'title' => '读者类型',
            'type' => 'list',
            'data_source' => '高年级学生,低年级学生,老师'
        ],
        [
            'title' => '读者状态',
            'type' => 'list',
            'data_source' => '停用,正常,注销,挂失'
        ],
        [
            'title' => '注册日期'
        ],
        [
            'title' => '学号'
        ]
    ]
];

//列表数据
//次序与类型必须与options对应
$data = [
    'DD123456',
    '张三',
    '女',
    '高年级学生',
    '停用',
    '2016-07-10',
    'G44532220060903492X'
];
$excel = new \QsExcel\Excel();
$excel->addBuild(new \QsExcel\Builder\ListBuilder($options, $data));
$excel->output('reader_import.xlsx');


//大数组数据,总字符串长度超过565,将会自动生成数据源sheet进行存储
$project_arrs = [
    .
    .
    .
];

$book_arrs = [
    .
    .
    .
];

$options  = [
    'row_count' => 500,
    'headers' => [
        [
            'title' => '读者编号',
        ],
        [
            'title' => '姓名',
        ],
        [
            'title' => '性别',
            'type' => 'list',
            'data_source' => '女,男'
        ],
        [
            'title' => '项目',
            'type' => 'list',
            'data_source' => implode(',', $project_arrs)
        ],
        [
            'title' => '推荐书籍',
            'type' => 'list',
            'data_source' => implode(',', $book_arrs)
        ],
        [
            'title' => '注册日期'
        ],
        [
            'title' => '学号'
        ]
    ]
];

$options1  = [
    'row_count' => 500,
    'headers' => [
        [
            'title' => '读者姓名',
        ],
        [
            'title' => '学校',
        ],
        [
            'title' => '性别',
            'type' => 'list',
            'data_source' => '女,男'
        ],
        [
            'title' => '项目',
            'type' => 'list',
            'data_source' => implode(',', $project_arrs)
        ],
        [
            'title' => '注册日期'
        ],
        [
            'title' => '学号'
        ]
    ]
];

$excel = new \QsExcel\Excel();
$excel->addBuild((new \QsExcel\ListBuilder($options))->setSheetName('test'));
$excel->addBuild((new \QsExcel\ListBuilder($options1))->setSheetName('test1'));
$excel->output('reader_import.xlsx');


[
    'title' => '性别',
    'type' => QsExcel\Builder\ListBuilder::LIST_TYPE,
    'data_source' => '男,女'
];

$arr = []; //大数组
[
    'title' => '性别',
    'type' => QsExcel\Builder\ListBuilder::LIST_TYPE,
    'data_source' => implode(',', $arr)
];

[
    'title' => '团队名',
    'type' => QsExcel\Builder\ListBuilder::LIST_TYPE,
    'data_source' => '团队信息!$B$2:$B$500'   // 团队信息为其他sheet的sheet名,前后两个列字母的必须一致,这个例子就是B
];

[
    'title' => '出生日期',
    'type' => QsExcel\Builder\ListBuilder::DATE_TYPE
];

$arr = [ '标签1', '标签2', ...];
[
    'title' => '标签',
    'type' => QsExcel\Builder\ListBuilder::MULTI_LIST_TYPE,
    'data_source' => implode(',', $arr)
];

$file = __DIR__ . '/excel.xls';  
$excel = new Excel();
//需要读取多少个sheet的数据,则设置多少个loader,可根据不同的sheet类型来针对性的设置不同的loader类型
//默认第一个loader读取index为0的sheet 第二个读取index为1的sheet,以此类推
$excel->setLoadFile($file);
$excel->addLoader(new ListLoader());
$excel->addLoader(new ListLoader());
$excel->addLoader(new ListLoader());
$excel->addLoader(new ListLoader());
$list = $excel->load();