moveek/excel-bundle

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

安装次数: 2,600

依赖者: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 9

类型:symfony-bundle

1.0.4 2022-06-07 10:51 UTC

This package is auto-updated.

Last update: 2024-09-07 16:34:21 UTC


README

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

安装

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

    composer require onurb/excel-bundle

或手动在 composer.json 中添加

    ...,
    "require": {
            ...,
            "onurb/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 文档