tanur/excel-bundle

Symfony Bundle,用于读取或写入 Excel 文件(包括 pdf、xlsx、odt),使用 phpoffice/phpspreadsheet 库(phpoffice/phpexcel 的替代品,已弃用)

维护者

详细信息

github.com/tanur/excel-bundle

源代码

安装次数: 1,221

依赖关系: 0

建议者: 0

安全性: 0

星标: 1

关注者: 0

分支: 9

类型:symfony-bundle

1.0.2 2023-11-02 13:06 UTC

This package is auto-updated.

Last update: 2024-08-31 00:32:24 UTC


README

Build Status Scrutinizer Code Quality Code Coverage License Latest Stable Version Total Downloads

安装

1 将以下内容添加到 composer.json 的 require 键中

    composer require tanur/excel-bundle

或手动在 composer.json 中添加

    ...,
    "require": {
            ...,
            "tanur/excel-bundle":     "~1.0"
        },
    ...

2 Symfony 3:在 app/AppKernel.php 中注册该 Bundle

    $bundles = array(
        // ...
        new \Onurb\Bundle\ExcelBundle\OnurbExcelBundle(),
    );

Symfony 4:使用 symfony flex,Bundle 应已自动注册

   // config/bundles.php
    return [
        // ...
        Onurb\Bundle\ExcelBundle\OnurbExcelBundle::class => ['all' => true],
    ];

使用方法

创建电子表格

$spreadsheet = $this->get('phpspreadsheet')->createSpreadsheet();

从现有文件创建电子表格

$spreadsheet = $this->get('phpspreadsheet')->createSpreadsheet('file.xlsx');

根据对象创建 Excel5 并写入文件

$writer = $this->get('phpspreadsheet')->createWriter($spreadsheet, 'Xls');
$writer->save('file.xls');

创建 Excel 2007 并创建 StreamedResponse

$writer = $this->get('phpspreadsheet')->createWriter($spreadsheet, 'Xlsx');
$response = $this->get('phpspreadsheet')->createStreamedResponse($writer);

创建带有图像的 Excel 文件

$writer = $this->get('phpspreadsheet')->createSpreadSheet();
$writer->setActiveSheetIndex(0);
$activesheet = $writer->getActiveSheet();
$drawingobject = $this->get('phpspreadsheet')->createSpreadsheetWorksheetDrawing();
$drawingobject->setPath('/path/to/image')
    ->setName('Image name')
    ->setDescription('Image description')
    ->setHeight(60)
    ->setOffsetY(20)
    ->setCoordinates('A1')
    ->setWorksheet($activesheet);

创建读取器

$reader = $this->get('phpspreadsheet')->createReader('Xlsx');

支持文件类型

类型区分大小写。支持类型包括

  • Xlsx:Excel 2007
  • Xls:Excel 5
  • Xml:Excel 2003 XML
  • Slk:符号链接(SYLK)
  • Ods:Libre Office(ODS)
  • Csv:CSV
  • Html:HTML

可选库可用于写入

  • Tcpdf
  • Mpdf
  • Dompdf

安装这些库

composer require tecnick.com/tcpdf
composer require mpdf/mpdf
composer require dompdf/dompdf

liuggio/Excelbundle 可移植性

对于已经使用 liuggio/ExcelBundle 并希望迁移到 phpspreadsheet 的用户,该 Bundle 应该直接兼容:保留了旧的 phpexcel 文件类型,已添加兼容性工厂,并且重新声明了 phpexcel 服务。

更多

请参阅官方 PhpSpreadsheet 文档