定制 fersot/excel-to 二次开发

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

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

fersot/excel-to

最新稳定版本:v2.0.0

Composer 安装命令:

composer require fersot/excel-to

包简介

Laravel package that converts Excel/CSV files to JSON, Collection, or Array — with fluent API, sheet selection, column filtering, and export support.

README 文档

README

A Laravel package for converting Excel and CSV files to JSON, Laravel Collections, or PHP Arrays — with a fluent API, multi-sheet support, column filtering, date formatting, and export capabilities.

Latest Version PHP Version License Tests

Installation

composer require fersot/excel-to

Laravel auto-discovers the service provider and facade automatically.

Optionally publish the config file:

php artisan vendor:publish --tag=excel-to-config

Quick Start

use Fersot\ExcelTo\ExcelTo;

// Static shortcuts (backwards compatible)
$array      = ExcelTo::array('path/to/file.xlsx');
$collection = ExcelTo::collection('path/to/file.xlsx');
$json       = ExcelTo::json('path/to/file.xlsx');

// Fluent API (recommended)
$data = ExcelTo::file('path/to/file.xlsx')
    ->sheet('Clientes')
    ->skipEmpty()
    ->only(['nombre', 'email', 'ciudad'])
    ->dateFormat('Y-m-d')
    ->toArray();

Reading Files

Load from path

ExcelTo::file('path/to/file.xlsx')->toArray();

Load from Laravel UploadedFile

ExcelTo::upload($request->file('excel'))->toCollection();

Load a CSV

ExcelTo::file('path/to/file.csv')->toArray();

Output Formats

$instance = ExcelTo::file('file.xlsx');

$instance->toArray();       // PHP array
$instance->toCollection();  // Illuminate\Support\Collection
$instance->toJson();        // JSON string
$instance->toJson(JSON_PRETTY_PRINT); // JSON with flags

Fluent Options

All options can be chained in any order.

Select a Sheet

// By name
ExcelTo::file('file.xlsx')->sheet('Sheet2')->toArray();

// By zero-based index
ExcelTo::file('file.xlsx')->sheet(1)->toArray();

With a single-sheet file the result is a flat array. With multiple sheets, each sheet is a key in the result.

Skip Empty Rows

ExcelTo::file('file.xlsx')->skipEmpty()->toArray();

// Disable explicitly
ExcelTo::file('file.xlsx')->skipEmpty(false)->toArray();

Custom Header Row

Useful when the first row is a title and headers start on row 2:

ExcelTo::file('file.xlsx')->headerRow(2)->toArray();

Date Format

// Default: 'd/m/Y'
ExcelTo::file('file.xlsx')->dateFormat('Y-m-d')->toArray();
ExcelTo::file('file.xlsx')->dateFormat('d-m-Y H:i')->toArray();

Filter Columns

// Keep only these columns
ExcelTo::file('file.xlsx')->only(['nombre', 'email'])->toArray();

// Exclude these columns
ExcelTo::file('file.xlsx')->except(['id', 'internal_code'])->toArray();

Exporting to Excel

use Fersot\ExcelTo\ExcelTo;

$data = [
    ['producto' => 'Widget', 'precio' => 9.99,  'stock' => 100],
    ['producto' => 'Gadget', 'precio' => 19.99, 'stock' => 50],
];

ExcelTo::fromArray($data)->export('storage/exports/report.xlsx');

// From a Collection
ExcelTo::fromCollection($collection)->export('storage/exports/report.xlsx');

Configuration

After publishing, edit config/excel-to.php:

return [
    'date_format' => 'd/m/Y',   // Default date output format
    'skip_empty'  => false,     // Skip empty rows by default
    'header_row'  => 1,         // Default header row number
];

Options set via the fluent API always override config values.

Full Example

use Fersot\ExcelTo\ExcelTo;

$report = ExcelTo::file(storage_path('imports/ventas_2024.xlsx'))
    ->sheet('Enero')
    ->headerRow(2)
    ->skipEmpty()
    ->only(['cliente', 'importe', 'fecha'])
    ->dateFormat('Y-m-d')
    ->toCollection();

$report->each(fn($row) => \Log::info($row));

Requirements

Dependency Version
PHP ^8.1
Laravel ^9.0 | ^10.0 | ^11.0 | ^12.0
PhpSpreadsheet ^1.29 | ^2.0

Testing

composer install
./vendor/bin/phpunit --testdox

25 tests covering all features.

Author

Hember Colmenareshemberfer@gmail.com · GitHub

Support Me ☕️

If you find this package useful, consider buying me a coffee.

Buy Me a Coffee

License

MIT — see LICENSE.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固