yectep/phpspreadsheet-bundle

一个用于与PHPOffice的PhpSpreadsheet库集成的Symfony扩展包

安装次数: 1,734,589

依赖者: 5

建议者: 0

安全性: 0

星标: 56

关注者: 4

分支: 24

公开问题: 6

类型:symfony-bundle

v1.1.0 2023-12-04 21:57 UTC

This package is auto-updated.

Last update: 2024-09-07 02:27:35 UTC


README

此扩展包将您的Symfony 4/5/6/7应用程序与PHPOffice PhpSpreadsheet生产力库集成。

要求

此扩展包除了每个PHPOffice库的先决条件外还需要

* PHP 7.2 or higher
* Symfony 4 or higher

注意:版本低于v1.0.0的标签(例如v0.2.0)不再受支持,因为PHP <= 7.1和Symfony <= 4.4都处于弃用状态。

安装

使用composer要求最新的稳定版本。

composer require yectep/phpspreadsheet-bundle

如果您不使用Flex,请在您的AppKernel.phpbundles.php文件中启用此扩展包。

$bundles = array(
    [...]
    new Yectep\PhpSpreadsheetBundle\PhpSpreadsheetBundle(),
);

使用

此扩展包启用了phpoffice.spreadsheet服务。

还可以参考官方的PHPOffice PhpSpreadsheet文档

createSpreadsheet()

创建一个空的\PhpOffice\PhpSpreadsheet\Spreadsheet对象,或者如果传递了一个可选的$filename,则使用\PhpOffice\PhpSpreadsheet\IOFactory自动检测并使用适当的IWriter类来读取文件。

// In your controller
$newSpreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$existingXlsx   = $this->get('phpoffice.spreadsheet')->createSpreadsheet('/path/to/file.xlsx');

createReader(string $type)

返回给定$type\PhpOffice\PhpSpreadsheet\Reader类的实例。

类型区分大小写。支持以下类型:

  • Xlsx:Excel 2007
  • Xls:Excel 5/BIFF (95)
  • Xml:Excel 2003 XML
  • Slk:符号链接(SYLK)
  • Ods:Open/Libre Office(ODS)
  • Csv:CSV
  • Html:HTML
$readerXlsx  = $this->get('phpoffice.spreadsheet')->createReader('Xlsx');
$spreadsheet = $readerXlsx->load('/path/to/file.xlsx');

createWriter(Spreadsheet $spreadsheet, string $type)

给定一个\PhpOffice\PhpSpreadsheet\Spreadsheet对象和一个写入器$type,返回该类型的\PhpOffice\PhpSpreadsheet\Writer类的实例。

除了上面的读取类型外,如果安装了适当的PHP库,还支持以下类型用于写入:

  • Tcpdf
  • Mpdf
  • Dompdf
$spreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Hello world');

$writerXlsx = $this->get('phpoffice.spreadsheet')->createWriter($spreadsheet, 'Xlsx');
$writerXlsx->save('/path/to/destination.xlsx');

路线图和贡献

欢迎贡献。Fork项目,完成后提交PR。

剩余的任务包括

  • 测试和测试覆盖率
  • TravisCI
  • 改进的文档

Symfony序列化器

如果您从Symfony Serializer组件 + CSV编码器迁移,可以使用以下代码:

$spreadsheet = $this->get('phpoffice.spreadsheet')->createSpreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle($this->filterVars['wareCategory']->getTitle());
$columnsMap = [];
$lineIndex = 2;
foreach ($data as $line) {
   foreach ($line as $columnName=>$columnValue) {
       if (is_int($columnIndex = array_search($columnName, $columnsMap))) {
           $columnIndex++;
       } else {
           $columnsMap[] = $columnName;
           $columnIndex = count($columnsMap);
       }
       $sheet->getCellByColumnAndRow($columnIndex, $lineIndex)->setValue($columnValue);
   }
   $lineIndex++;
}
foreach ($columnsMap as $columnMapId=>$columnTitle) {
   $sheet->getCellByColumnAndRow($columnMapId+1, 1)->setValue($columnTitle);
}
$writer = $this->get('phpoffice.spreadsheet')->createWriter($spreadsheet, 'Xlsx');
ob_start();
$writer->save('php://output');
$excelOutput = ob_get_clean();

return new Response(
   $excelOutput,
   200,
   [
       'content-type'        =>  'text/x-csv; charset=windows-1251',
       'Content-Disposition' => 'attachment; filename="price.xlsx"'
   ]
);