quytvoday/htmlphpexcel

基于 PHPExcel 的 PHP 库,用于将 HTML 表格转换为 Excel 文件,包括样式。

1.3 2024-08-19 18:43 UTC

This package is auto-updated.

Last update: 2024-09-19 19:16:37 UTC


README

Build Status Scrutinizer Code Quality

这是一个基于 PhpSpreadsheet 的 PHP 库,它简化了将 HTML 表格转换为 Excel 文件的过程。它允许在 HTML 模板中使用特定属性直接进行样式设置。

安装

将 HtmlPhpExcel 添加到您的 composer.json 文件中

composer require quytvoday/htmlphpexcel

简单示例

<?php

require_once('../vendor/autoload.php');

$html = '<table><tr><th>Column A</th><th>Column B</th></tr><tr><td>Value A</td><td>Value B</td></tr></table>';
$htmlPhpExcel = new \Ticketpark\HtmlPhpExcel\HtmlPhpExcel($html);

// Create and output the excel file to the browser
$htmlPhpExcel->process()->output();

// Alternatively create the excel and save to a file
$htmlPhpExcel->process()->save('myFile.xlsx');

// or get the \PhpOffice\PhpSpreadsheet\Spreadsheet object to do further work with it
$phpExcelObject = $htmlPhpExcel->process()->getExcelObject();

要查看具有样式选项的更复杂示例,请参阅 示例目录

样式

支持特定的 HTML 属性,以便对 Excel 输出进行样式设置。这些属性期望内容为 json_encoded。

  • _excel-styles
    支持 PhpSpreadsheet 的 applyFromArray() 方法中所有可能的设置(也请参阅这里)。

示例

<table>
    <tr>
        <td _excel-styles='{"font":{"size":16,"color":{"rgb":"FF0000"}}}'>Foo</td>
    </tr>
</table>
  • _excel-dimensions
    支持改变行(当应用于 <tr><td>)或列(当应用于 <td>)的尺寸。

示例

<table>
    <tr _excel-dimensions='{"row":{"rowHeight":50}}'>
        <td _excel-dimensions='{"column":{"width":20}}'>Foo</td>
    </tr>
</table>
  • _excel-explicit
    支持应用显式的单元格值类型。

示例

<table>
    <tr>
        <td _excel-explicit='PhpSpreadsheet_Cell_DataType::TYPE_STRING'>0022</td>
    </tr>
</table>