定制 bayareawebpro/laravel-simple-csv 二次开发

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

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

bayareawebpro/laravel-simple-csv

最新稳定版本:v2.2.1

Composer 安装命令:

composer require bayareawebpro/laravel-simple-csv

包简介

A simple CSV importer/ exporter for Laravel.

README 文档

README

https://packagist.org/packages/bayareawebpro/laravel-simple-csv

Features

  • Import to LazyCollection.
  • Export from Collection, LazyCollection, Iterable, Generator, Array.
  • Low(er) Memory Consumption by use of LazyCollection Generators.
  • Uses Native PHP SplFileObject.
  • Facade Included.

Installation

Require the package and Laravel will Auto-Discover the Service Provider.

composer require bayareawebpro/laravel-simple-csv

Usage:

Invokable classes can be passed to the import method allowing you to customize how each row is processed. Two classes to handle numerics and null values have been supplied.

use BayAreaWebPro\SimpleCsv\SimpleCsv;
use BayAreaWebPro\SimpleCsv\Casts\EmptyValuesToNull;
use BayAreaWebPro\SimpleCsv\Casts\NumericValues;

$lazyCsvCollection = SimpleCsv::import(storage_path('collection.csv'), [
    EmptyValuesToNull::class,
    NumericValues::class,
]);

Invokable Classes

Dependency Injection: Invokable classes can typehint required dependencies in a constructor method when defined.

<?php declare(strict_types=1);

namespace App\Csv\Casts;

use Carbon\Carbon;

class Timestamps
{
    /** Invoked for each row in import collection. */
    public function __invoke(array $item): array
    {
        foreach ($item as $key => $value){
            if(in_array($key, ['created_at', 'updated_at'])){
                $item[$key] = Carbon::parse($value);
            }
        }
        return $item;
    }
}

Export to File

use BayAreaWebPro\SimpleCsv\SimpleCsv;

// Collection
SimpleCsv::export(
    Collection::make(...),
    storage_path('collection.csv')
);

// LazyCollection
SimpleCsv::export(
    LazyCollection::make(...),
    storage_path('collection.csv')
);

// Generator (Cursor)
SimpleCsv::export(
    User::query()->where(...)->limit(500)->cursor(),
    storage_path('collection.csv')
);

// Array
SimpleCsv::export(
    [...],
    storage_path('collection.csv')
);

Export Download Stream

use BayAreaWebPro\SimpleCsv\SimpleCsv;

return SimpleCsv::download([...], 'download.csv');

Override Options

use Illuminate\Support\Facades\Config;

Config::set('simple-csv.delimiter', ...);
Config::set('simple-csv.enclosure', ...);
Config::set('simple-csv.escape', ...);

Or, Create a Config File

config/simple-csv.php

return [
    'delimiter' => '?',
    'enclosure' => '?',
    'escape'    => '?',
];

File Splitting Utility

A file splitting utility has been included that will break large CSV files into chunks (while retaining column headers) which you can move/delete after importing. This can help with automating the import of large data sets.

Tip: Find your Bash Shell Binary Path: which sh

/bin/sh vendor/bayareawebpro/laravel-simple-csv/split-csv.sh /Projects/laravel/storage/big-file.csv 5000

File Output:
/Projects/laravel/storage/big-file-chunk-1.csv (chunk of 5000)
/Projects/laravel/storage/big-file-chunk-2.csv (chunk of 5000)
/Projects/laravel/storage/big-file-chunk-3.csv (chunk of 5000)
etc...

Speed Tips

  • Using Lazy Collections is the preferred method.
  • Using the queue worker, you can import a several thousand rows at a time without much impact.
  • Be sure to use "Database Transactions" and "Timeout Detection" to insure safe imports.
  • Article: How to Insert & Update Many at Once

统计信息

  • 总下载量: 51.49k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-29