ank/phpexcel

3.0.1 2020-08-20 12:38 UTC

This package is auto-updated.

Last update: 2024-09-20 21:52:42 UTC


README

导出Excel的使用方法

首先查询出要导出的列表数据

//从数据库中查询出数据设置给data;
$conf=[
        'data'     => [
            ['id' => 123, 'title' => '标题'],
        ],
        //下载的文件名字不用加后缀
        'filename' => 'data',
        //定义excel中要使用的数据字段和字段对应的标题
        'field'    =>[
            'id' => '序号',
            'title' => '文章标题',
            '#link#url'=>'http://www.baidu.com',
            '#pic#pic'=>'./1.png'
        ],
    ];

id,title等字段前面可以添加一些特殊格式的标识 #标识# 来插入对应的数据,例如 #pic#id 插入图片,id的值会被识别为图片的路径;#link#url 插入链接,url的值为链接地址

导入Excel数据的方法

导入数据后,第一行默认作为字段名,真实数据从第二行开始,第一列数据不能为空,如果第一列为空值,则认为Excel内容结束。格式为

title   name    price
标题1   名字1   130
标题2   名字2   140

导入后的格式为一个二维数组

array(
array('title'=>"标题1",'name'=>'名字1','price'=>'130'),
array('title'=>"标题2",'name'=>'名字1','price'=>'140'),
)

首先创建一个表单

<form action="" method="post">
<input type="file" name="excel">
<input type="submit" value="导入">
</form>

//使用方法

$excel=new \ank\Excel();

//参数一是excel文件路径如果存在则直接解析,参数二是上传的表单name参数一为null的情况下自动从文件域为excel的键读取文件内容
$excel->importExcel('e:/upload/example.xls','excel');


//参数是一个配置项如上面导出的配置一样
$excel->downloadExcel($conf);
//更多用法可以参考example.php中的使用方法