定制 mahmud/sheet 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mahmud/sheet

Composer 安装命令:

composer require mahmud/sheet

包简介

Excel and CSV file reader

README 文档

README

Build Status Latest Stable Version License composer.lock

A clean and beautiful API to read Excel/CSV sheet. This is a wrapper around box/spout package.

Installation

composer require mahmud/sheet

Requirements

  • php: ^7.1.3
  • box/spout: ^3.0

Usage

Simple Example

Let's assume we have a csv file like this.

ID Name Age
1 Mahmud 27
2 Mohor 26
3 Ayman 1
use Mahmud\Sheet\SheetReader;

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")                        // Optional: You can set delimiter for CSV file
            ->ignoreRow(0)                          // Optional: Skip the header row
            ->columns(['id', 'name', 'age'])        // Arbitary column name that will be mapped sequentially for each row
            ->onEachRow(function($row, $index){
                // This callback will be executed for each row
                var_dump($row);     // Current row in associative array
                var_dump($index);   // Current index of the row
            })->read();

Middleware

You can modify data of each row with middleware. See the following example

use Mahmud\Sheet\SheetReader;

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(function($row, $index){
                $row['age'] = $row['age'] . " Years";
                
                return $row;
            })
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

Another example using class as middleware

class AgeMiddleware{
    public function handle($row, $index) {
        $row['age'] = $row['age'] . " Years";
    
        return $row;
    }
}

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(new AgeMiddleware)
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

Also you can pass array of middlewares

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware([
                new AgeMiddleware,
                new AnotherMiddleware,
            ])
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

If you return null from middleware, That row will be skipped and won't pass to onEachRow handler.

SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
            ->delimiter(",")
            ->ignoreRow(0)
            ->columns(['id', 'name', 'age'])
            ->applyMiddleware(function($row){
                if($row['id'] == 1){
                    return null;
                }
                
                return $row;
            })
            ->onEachRow(function($row, $index){
                var_dump($row);
            })->read();

Count total rows

$total = SheetReader::makeFromCsv('/path-to-csv-file/example-file.csv')
                        ->totalRows();
                        
var_dump($total);       // 4

统计信息

  • 总下载量: 134
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 30
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固