承接 lab36/excel-report 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

lab36/excel-report

Composer 安装命令:

composer require lab36/excel-report

包简介

Customizable Excel reports in Laravel

README 文档

README

Latest Version on Packagist Total Downloads

This package allows you to generate Microsoft Excel files from any collection or array. You can easily apply formatting, styles or themes. It comes bundled with beautiful themes ready to use.

This php library is a wrapper around PhpSpreadsheet library.

Motivation

We needed to export beautiful excel reports and with as little boilerplate as possible. PhpSpreadsheet does a good job working with excel files and we needed something on top just to avoid the repetitive work.

✨ Features

  • Easily export collection/array to Excel. Take your Laravel collection/array and export them directly to an Excel document.

  • Beautiful themes ready to go. Beautiful templates easy customizable already prepared for you.

  • Customize any cell. Hooks to easily customize any cell based on values or coordinates.

Basic Usage

composer require excel-report/excel-report

Just create a file in anywhere in your app folder or use an artisan command:

php artisan excel:report ProjectDetailsExcelReport

This by default will create the file ProjectDetailsExcelReport.php with folder app\ExcelReport.

<?php
namespace App\ExcelReport;

use Lab36\ExcelReport\ExcelReport;

class ProjectDetailsExcelReport extends ExcelReport
{
    public function columnMappings(): array
    {
        return [
            'project' => 'Project name',
            'start_date' => 'Project date',
            'cost' => 'Cost',
            'type' => 'Type',
            'state'=> 'State',
            'client_name'=>'Client name',
            'client_address'=>'Client address',
        ];
    }
}

In your controller:

<?php

namespace App\Http\Controllers;

use App\ExcelReport\ProjectDetailsExcelReport;
use App\Http\Controllers\Controller;
use Faker\Factory;


class ReportController extends Controller
{
    public function exportProjects()
    {
        $faker = Factory::create();

         $project_data = collect([
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(2500, 3000),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(1000, 2000),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
            [
                'project' => $faker->lastName,
                'start_date' => $faker->date('Y-m-d'),
                'cost' => $faker->numberBetween(4200, 4500),
                'type' => $faker->numberBetween(0, 3),
                'state'=> $faker->numberBetween(0,1),
                'client_name'=>$faker->name,
                'client_address'=>$faker->address,
            ],
        ]);

     
        return ProjectDetailsExcelReport::fromCollection(
             'Project details',
             $project_data
        )->download();
    }
}

banner

For more examples follow this link.

Installation

You can install the package via composer:

composer require excel-report/excel-report

and then publish the config

php artisan vendor:publish --provider="Lab36\ExcelReport\ExcelReportServiceProvider"

Options

First thing you need the ExcelReport file. It's just a php class that extends ExcelReport. You can use

php artisan excel:report ClassName

Column mappings

The ExcelReport file must have a columnMappings function. This function maps array/collection values to Spreadsheet columns.

Exemple: Let's say we have an array

 $employee_data = [
        [
            'name' => $faker->name,
            'start_date' => $faker->date('Y-m-d'),
            'employee_cost' => $faker->numberBetween(100, 200),
        ],
        [
            'name' => $faker->name,
            'start_date' => $faker->date('Y-m-d'),
            'employee_cost' => $faker->numberBetween(100, 200),
        ],
    ];

the columnMappings function would be:

public function columnMappings(): array
    {
        return [
            'name' => __('Name'),
            'start_date' => 'Start date',
            'employee_cost' => 'Employee cost',
        ];
    }

columnMappings values would become the header for the excel report. If you want to ignore a column from the array just omit it from columnMappings. You can also use translations here.

Column alignment

By default PhpSpreadsheet does a good job aligning data. If you want to provide some exceptions you can implement columnAlignment function and specify the alignment.

Possible values:

  • LEFT
  • RIGHT
  • CENTER
  • HORIZONTAL_GENERAL
  • HORIZONTAL_LEFT
  • HORIZONTAL_RIGHT
  • HORIZONTAL_CENTER
  • HORIZONTAL_CENTER_CONTINUOUS
  • HORIZONTAL_JUSTIFY
  • HORIZONTAL_FILL
  • HORIZONTAL_DISTRIBUTED
    public function columnAlignment(): array
    {
        return [
            'start_date' => 'CENTER',
        ];
    }

You can specify only the columns for which you need to change the alignment.

Column formats

You can specify column formats by implementing the function columnFormats

  • NUMERIC
  • TEXT
  • DATE_DD-MM-YYYY
  • NUMERIC_FORMATTED
  • DATE_YYYY-MM-DD
  • CURRENCY_LEI
  • FORMAT_GENERAL
  • FORMAT_TEXT
  • FORMAT_NUMBER
  • FORMAT_NUMBER_00
  • FORMAT_NUMBER_COMMA_SEPARATED1::FORMAT_NUMBER_COMMA_SEPARATED1,
  • FORMAT_NUMBER_COMMA_SEPARATED2::FORMAT_NUMBER_COMMA_SEPARATED2,
  • FORMAT_PERCENTAGE
  • FORMAT_PERCENTAGE_00
  • FORMAT_DATE_YYYYMMDD2
  • FORMAT_DATE_YYYYMMDD
  • FORMAT_DATE_DDMMYYYY
  • FORMAT_DATE_DMYSLASH
  • FORMAT_DATE_DMYMINUS
  • FORMAT_DATE_DMMINUS
  • FORMAT_DATE_MYMINUS
  • FORMAT_DATE_XLSX14
  • FORMAT_DATE_XLSX15
  • FORMAT_DATE_XLSX16
  • FORMAT_DATE_XLSX17
  • FORMAT_DATE_XLSX22
  • FORMAT_DATE_DATETIME
  • FORMAT_DATE_TIME1
  • FORMAT_DATE_TIME2
  • FORMAT_DATE_TIME3
  • FORMAT_DATE_TIME4
  • FORMAT_DATE_TIME5
  • FORMAT_DATE_TIME6
  • FORMAT_DATE_TIME7
  • FORMAT_DATE_TIME8
  • FORMAT_DATE_YYYYMMDDSLASH
  • FORMAT_CURRENCY_USD_SIMPLE
  • FORMAT_CURRENCY_USD
  • FORMAT_CURRENCY_EUR_SIMPLE
  • FORMAT_CURRENCY_EUR
    public function columnFormats(): array
    {
        return [
            'start_date' => 'DATE_YYYY-MM-DD',
            'employee_cost' => 'NUMERIC_FORMATTED',
        ];
    }

You can also specify any PhpSpreadsheet format by using the function addColumnFormats(), which will merge default formats with the one specified.

Column width

You can define any custom column width in character units implementing the following function.

    public function columnWidth(): array
    {
        return [
            'name' => '40',
        ];
    }

By default PhpSpreadsheet tries to guess the correct width for the column but there are cases when they become too long so we need to adjust. When a column gets smaller width, data in that column will wrap. Depending on your settings (default_row_height) rows will adjust to wrapping.

Special row positioning

You can override the default positioning for title, header and filters. For this you must call the coresponding setters.

In your Controller you can have

 return ProjectDetailsExcelReport::fromCollection(
             'Project details',
             $project_data
        )
        ->setTitleRowNo(2)
        ->setFilterRowNo(5)
        ->setHeaderRowNo(6)
        ->download();

Choosing a theme

This package comes with two themes. The default theme is set in the config file. You can create other themes by implementing ExcelThemeInterface.

Filter reports

This filters represent the values used to obtain data for the report. If the filter doesn't have a corresponding column it will appear after the last column.

Filters are an array in form:

[column_name => filter_value, ...]

$project_data = Project::where('name','=','John')
                        ->where('start_date', '=', '2018-01-01')
                        ->get();

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->setFilters([
                                            'name' => 'John',
                                            'start_date' => '2018-01-01',
                                            ])
                                ->download();

Download/store

You can choose to download or store on the server the created file. You can provide the file name to be used.

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->download('Project details '.Carbon::now()->toDateString());

To store the file you must specify the path

return ProjectDetailsExcelReport::fromCollection(
                                                 'Project details',
                                                 $project_data)
                                ->store('Project details '.Carbon::now()->toDateString(), public_path());

OverrideCell

To override any cell of the report you can implement the overrideCell method in you ExcelReport file with the following parameters:

  • active_sheet
  • current_row_no
  • column_no
  • value
  • row_data
  • column_name
public function overrideCell(Worksheet $active_sheet, int $current_row_no, int $column_no, $value, array $row_data, string $column_name)
{
    if ($column_name == 'order_type' && is_numeric($value)) {
        $active_sheet->setCellValue(Coordinate::stringFromColumnIndex($column_no).$current_row_no,
                                    config('enums.order_type')[$value]);
    }
}

Here you can override data, formatting, styling, alignment, etc. You can use any PhpSpreadsheet function available for the active sheet.

If you are not using a flat array with overrideCell you can pick the values that you want to appear in your cell. You can check the example here.

Config file

In the config file you can define

  • excel file metadata
  • namespace used for artisan command
  • default theme
  • default row height

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

lab36/excel-report 适用场景与选型建议

lab36/excel-report 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 281 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「excel」 「themes」 「export」 「laravel」 「excel-report」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lab36/excel-report 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lab36/excel-report 我们能提供哪些服务?
定制开发 / 二次开发

基于 lab36/excel-report 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-04