承接 denisok94/symfony-export-xlsx 相关项目开发

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

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

denisok94/symfony-export-xlsx

Composer 安装命令:

composer require denisok94/symfony-export-xlsx

包简介

Symfony export for excel(.xlsx)

README 文档

README

The main idea is taken from XlsxWriter.php.

Except:

  • PHPOffice\PHPExcel has been replaced with PHPOffice\PhpSpreadsheet;
  • The class is used as a service to be able to use it anywhere and make changes to the file.
  • The XlsxService class is completely independent and can be used outside of Symfony.

license-MIT-green

  1. Install
  2. Use Service
  3. Customization
  4. Use in SonataAdmin Export
  5. Errors

Install

Run:

composer require --prefer-dist denisok94/symfony-export-xlsx
# or
php composer.phar require --prefer-dist denisok94/symfony-export-xlsx

or add to the require section of your composer.json file:

"denisok94/symfony-export-xlsx": "*"
composer update
# or
php composer.phar update

Use XlsxService

Method Parameters Return Description
setFile() string self
setDateTimeFormat() string self
open() - -
write() array -
close() - -
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use \Denisok94\SymfonyExportXlsxBundle\Service\XlsxService;

class ExportController extends AbstractController
{
    /** @var XlsxService */
    private $export;
    /**
     * @param XlsxService $export
     */
    public function __construct(XlsxService $export)
    {
        $this->export = $export;
    }
    /**
     * @return Response
     */
    public function index(): Response
    {
        $fileName = 'my_first_excel.xlsx';
        $temp_file = tempnam(sys_get_temp_dir(), $fileName);
        $this->export->setFile($temp_file)->open();
        $test = [
            ['header1' => 'value1', 'header2' => 'value2', 'header3' => 'value3'],
            ['header1' => 'value4', 'header2' => 'value5', 'header3' => 'value6']
        ];
        foreach ($test as $line) {
            $this->export->write($line);
        }
        $this->export->close();
        return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
    }
}

Example of a result

Customization

Method Return Official Documentation
getProperties() Class Properties https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Document-Properties.html
getSpreadsheet() Class Spreadsheet https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet.html
getActiveSheet() Class Worksheet https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet-worksheet.html
use \Denisok94\SymfonyExportXlsxBundle\Service\XlsxService;
/** @var XlsxService */
private $export;
/**
 * @param XlsxService $export
 */
public function __construct(XlsxService $export)
{
    $this->export = $export;
}
/**
 * @return Response
 */
public function index(): Response
{
    $fileName = 'my_first_excel.xlsx';
    $temp_file = tempnam(sys_get_temp_dir(), $fileName);
    $this->export->setFile($temp_file)->open();

    //
    $this->export->getProperties()
        ->setCreator('Denis')
        ->setLastModifiedBy('Denis')
        ->setSubject('my_first_excel')
        ->setTitle('my_first_excel');

    $test = [
        ['header1' => 'value1', 'header2' => 'value2', 'header3' => 'value3'],
        ['header1' => '4', 'header2' => '5', 'header3' => '=A3+B3']
    ];
    // header1 (A1) / header2 (B1) / header3 (C1) 
    // value1 (A2) / value2 (B2) / value3 (C2) 
    // 4 (A3) / 5 (B3) / =A3+B3 (C3) 
    foreach ($test as $line) {
        $this->export->write($line);
    }

    // https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Worksheet-Worksheet.html#method_mergeCells
    $this->export->getActiveSheet()->mergeCells('B2:C2');

    // https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Style-Borders.html
    $this->export->getActiveSheet()
        ->getStyle('A2:C3')->getBorders()->applyFromArray(['allBorders' => ['borderStyle' => 'thin', 'color' => ['rgb' => '000000']]]);

    // https://phpoffice.github.io/PhpSpreadsheet/classes/PhpOffice-PhpSpreadsheet-Style-Color.html
    $this->export->getActiveSheet()
        ->getStyle('C3')->getFont()->getColor()->applyFromArray(['rgb' => 'FF0000FF']);

    $this->export->close();

    return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}

Example of a result

Table class

use Denisok94\SymfonyExportXlsxBundle\DBAL\Table as T;
$worksheet = $this->export->getActiveSheet();
//
$value = $worksheet->getCell([T::A, 1])->getValue(); // A:1
$worksheet->getCell([T::B, 2])->setValue($value); // B:2
//
$x = T::B;
while ($x <= T::K) {
    try {
        // code...
        $worksheet->getCell([$x, $y])->setValue($value);
    } catch (\Throwable $th) {
        throw new \RuntimeException('error set "' . T::getLetter($x) . ':' . $y . '":' . $th->getMassage());
    }
    $x++;
}

Use in SonataAdmin Export

Install if missing SonataExporterBundle

composer require sonata-project/exporter

Minimum version doctrine/orm: 2.8

add in config:

# ~config/packages/sonata_exporter.yaml
services:
  sonata.exporter.writer.xlsx:
    class: Denisok94\SymfonyExportXlsxBundle\Writer\XlsxWriter
    arguments: ["php://output"]
    tags:
      - { name: sonata.exporter.writer }

add in YourAdmin class according to the documentation:

public function getExportFormats(): array
{
    return ['xlsx'];
}

and if you need to configure the fields and their translation

protected function configureExportFields(): array
{
    // example:
    return [
        $this->trans('export.title') => 'title',
        $this->trans('export.anons') => 'text',
        $this->trans('export.date') => 'date'
    ];
}

Errors

If you see the error:

Attempted to call an undefined method named "toIterable" of class "Doctrine\ORM\Query"

Then make sure that the doctrine/orm version is 2.8 or higher.

You may need to update:

  • sonata-project/admin-bundle: ~3.*
  • sonata-project/doctrine-orm-admin-bundle: ~3.*
  • doctrine/doctrine-bundle: ~^2.3

denisok94/symfony-export-xlsx 适用场景与选型建议

denisok94/symfony-export-xlsx 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.06k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 denisok94/symfony-export-xlsx 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-11