sukohi/fluent-csv

一个Laravel包,用于生成或保存使用SJIS-win等编码的csv文件。

3.0.5 2020-09-25 21:17 UTC

This package is auto-updated.

Last update: 2024-08-26 05:39:29 UTC


README

一个允许你生成或保存已编码csv文件的Laravel包。
此包在Laravel 5.7下维护。

安装

执行composer命令。

composer require sukohi/fluent-csv:3.*

使用方法

下载

$csv_data = [   // UTF-8 
    ['データ 1-1', 'データ 1-2', 'データ 1-3'],
    ['データ 2-1', 'データ 2-2', 'データ 2-3'],
    ['データ 3-1', 'データ 3-2', 'データ 3-3'],
];
$to_encoding = 'SJIS-win';
$fluent = \FluentCsv::setData($csv_data, $to_encoding);
return $fluent->download('テスト.csv');    // File name can be multi-byte character.

保存

$csv_data = [   // UTF-8 
    ['データ 1-1', 'データ 1-2', 'データ 1-3'],
    ['データ 2-1', 'データ 2-2', 'データ 2-3'],
    ['データ 3-1', 'データ 3-2', 'データ 3-3'],
];
$fluent = \FluentCsv::setData($csv_data, 'SJIS-win');

if($fluent->save(storage_path('app/public/test.csv'))) {

    echo 'Complete!';

}

循环中添加数据

$items = \App\Item::get();
$fluent = \FluentCsv::setEncoding('SJIS-win');

foreach($items as $item) {

    $fluent->addData($item->only(['id', 'name']));

}

return $fluent->download('test.csv');

清除数据

$fluent->clearData();

检索

// Basic way
$path = '/PATH/TO/YOUR/CSV/FOLDER/test.csv';
$data = \FluentCsv::parse($path);

// with encoding
$path = '/PATH/TO/YOUR/CSV/FOLDER/test.csv';
$from_encoding = 'sjis-win';
$data = \FluentCsv::parse($path, $from_encoding);

许可证

此包受MIT许可证许可。

版权所有 2017 Sukohi Kuhoh