2017 © Pedro Peláez
 

library excel

excel

image

pfinal/excel

excel

  • Friday, June 8, 2018
  • by pfinal
  • Repository
  • 4 Watchers
  • 42 Stars
  • 293 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 1 Open issues
  • 7 Versions
  • 42 % Grown

The README.md

Excel 处理

安装

composer require pfinal/excel

使用示例

导入Excel

<?php

include 'vendor/autoload.php'; // 如果在框架中通常不需要

use PFinal\Excel\Excel;

date_default_timezone_set('PRC');

$data = Excel::readExcelFile('./1.xlsx', ['id' => '编号', 'name' => '姓名', 'date' => '日期']);

//处理日期
array_walk($data, function (&$item) {
    $item['date'] = Excel::convertTime($item['date'], 'Y-m-d');
});

var_dump($data);

//如果数据量大,建议用csv格式
$data = Excel::readExcelFile('./1.csv', ['id' => '编号', 'name' => '姓名', 'date' => '日期'], 1, 1, '', 'GBK');

Excel中的数据:, (*1)

编号 姓名 日期
1 张三 2017/7/18
2 李四 2017/7/19
3 王五 2017/7/20

导入得到结果如下:, (*2)

$data = [
    ['id'=>1,'name'=>'张三', 'date'=>'2017-07-18'],
    ['id'=>1,'name'=>'李四', 'date'=>'2017-07-19'],
    ['id'=>1,'name'=>'王五', 'date'=>'2017-07-20'],
];

导出到Excel文件

$data = [
    ['id' => 1, 'name' => 'Jack', 'age' => 18, 'date'=>'2017-07-18'],
    ['id' => 2, 'name' => 'Mary', 'age' => 20, 'date'=>'2017-07-18'],
    ['id' => 3, 'name' => 'Ethan', 'age' => 34, 'date'=>'2017-07-18'],
];

$map = [
  'title'=>[
        'id' => '编号',
        'name' => '姓名',
        'age' => '年龄',
    ],
];

$file = 'user' . date('Y-m-d');

//浏览器直接下载
Excel::exportExcel($data, $map, $file, '用户信息');

//保存到磁盘文件中
//Excel::toExcelFile($data, $map, $file, '用户信息');


//分块导出到CSV文件 (如果中文乱码,可输出为GBK字符集,将UTF-8改为GBK即可)
Excel::chunkExportCSV($map, './temp.csv', function ($writer) {

     DB::select('user')->orderBy('id')->chunk(100, function ($users) use ($writer) {
         /**  \Closure $writer */
         $writer($users);
     });
}, 'UTF-8');

The Versions

08/06 2018

dev-master

9999999-dev http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

08/06 2018

v1.1.4

1.1.4.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

19/09 2017

v1.1.3

1.1.3.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

14/09 2017

v1.1.2

1.1.2.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

12/09 2017

v1.1.1

1.1.1.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

12/09 2017

v1.1.0

1.1.0.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang

18/07 2017

v1.0.0

1.0.0.0 http://www.pfinal.cn

excel

  Sources   Download

MIT

The Requires

 

by Zou Yiliang