menhir2015/htmlphpexcel

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

dev-master 2023-06-13 09:10 UTC

This package is auto-updated.

Last update: 2024-09-14 16:50:25 UTC


README

Build Status

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

安装

将HtmlPhpExcel添加到您的composer.json中

composer require ticketpark/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>
  • _excel-comment
    为单元格添加注释。

示例

<table>
    <tr>
        <td _excel-comment='Comment content'>Cell value</td>
    </tr>
</table>