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
XlsxServiceclass is completely independent and can be used outside of Symfony.
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); } }
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); }
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 denisok94/symfony-export-xlsx 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
Bulk export of sylius resources
The bundle for easy using json-rpc api on your project
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
统计信息
- 总下载量: 8.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-11