百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

phpspreadsheet-helper

> 编程语言
开源

Ưμ㼯A

382 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Ưμ㼯A

PHPSpreadsheet Helper

PHP Excel Helper - Write and read Spreadsheet with easy way based on PhpSpreadsheet This library is a helper that encapsulate PhpSpreadsheet (Documentation) for simple usage.


OUTLINE

  • Demonstration
  • Installation
  • Requirements
  • Usage
    • Import & Export
      • newSpreadsheet()
      • output()
      • save()
    • Get Rows
      • getRow()
      • getRows()
    • Add Rows
      • addRow()
      • addRows()
      • Attributes
    • PhpSpreadsheet Original Usage Integration
      • Inject PhpSpreadsheet
      • Extract PhpSpreadsheet
    • Merge Cells
    • Multiple Sheets
      • setSheet()
      • getSheet()
    • Map of Coordinates & Ranges
    • Style Attributes
    • Columns Format
    • All Cells Format
      • setStyle()
      • setWrapText()
      • setAutoSize()
  • Setting
    • setTimezone()
  • Limitations
    • Performance Issue

DEMONSTRATION

Write to Excel

Output an Excel file to browser for download:

php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    ->addRow(['ID', 'Name', 'Email'])
    ->addRows([
        ['1', 'Nick','[email protected]'],
        ['2', 'Eric','eric@.....'],
    ])
    ->output('My Excel');

Read from Excel

Import above excel file and return two-dimensional array data contained rows > columns spread sheet:

php
$data = \yidas\phpSpreadsheet\Helper::newSpreadsheet('/tmp/My Excel.xlsx')
    ->getRows();
    
print_r($data);

Output result:

Array
(
    [0] => Array
        (
            [0] => ID
            [1] => Name
            [2] => Email
        )

    [1] => Array
        (
            [0] => 1
            [1] => Nick
            [2] => [email protected]
        )

    [2] => Array
        (
            [0] => 2
            [1] => Eric
            [2] => eric@.....
        )

)

REQUIREMENTS

This library requires the following:

  • Dependent on PhpSpreadsheet
    • PHP 5.6.0+
    • PHP extension php-zip enabled
    • PHP extension php-xml enabled
    • PHP extension php-gd2 enabled (if not compiled in)

INSTALLATION

Run Composer in your project:

composer require yidas/phpspreadsheet-helper

Then you could call it after Composer is loaded depended on your PHP framework:

php
require __DIR__ . '/vendor/autoload.php';

\yidas\phpSpreadsheet\Helper::newSpreadsheet();

USAGE

Import & Export

Simpliy read an Excel file then output to browser:

php
\yidas\phpSpreadsheet\Helper::newSpreadsheet('/tmp/excel.xlsx')
    ->addRow(['Modified A1'])
    ->output();

newSpreadsheet()

New or load an PhpSpreadsheet object

php
public static array newSpreadsheet(object|string $spreadSheet=null)

output()

Output file to browser

php
public static void output(string $filename='excel', string $format='Xlsx')

$format list: Xlsx, Xls, Html, Csv, Ods

save()

Save as file

php
public static string save(string $filename='excel', string $format='Xlsx')

Example:

php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    ->addRow(['Add A1'])
    ->save("/tmp/save");
// /tmp/save.xlsx

Get Rows

getRow()

Get data of a row from the actived sheet of PhpSpreadsheet

php
public static array getRow(boolean $toString=true, array $options=[], callable $callback=null)

Example:

php
use \yidas\phpSpreadsheet\Helper;

$row1 = Helper::newSpreadsheet($filepath)
    ->getRow();

$row2 = Helper::getRow();

print_r($row1);
print_r($row2);

Example of fetching content per each row (Example Code):

php
$helper = \yidas\phpSpreadsheet\Helper::newSpreadsheet($filepath);

while ($row = $helper->getRow()) {
    // Each row data process
}

getRows()

Get rows from the actived sheet of PhpSpreadsheet

php
public static array getRows(boolean $toString=true, array $options=[], callable $callback=null)

Example of getRows()

Add Rows

addRow()

Add a row to the actived sheet of PhpSpreadsheet

php
public static self addRow(array $rowData, array $rowAttributes=null)

$rowData value: An array contains string or array of attributes for each cell
$rowAttributes value: string or array of attributes for each cell of a row

Example of addRow()

Example of setting attributes for each cell:

php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    ->addRow([['value'=>'ID'], ['value'=>'Name'], ['value'=>'Email']])
    ->addRow(['ID', 'Name', 'Email']);

Example of setting attributes for each row:

php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    // Set width as 25 to all cells of this row
    ->addRow([['value'=>'ID'], ['value'=>'Name'], ['value'=>'Email']], ['width'=>25]);

addRows()

Add rows to the actived sheet of PhpSpreadsheet

php
public static self addRows(array $data, array $rowAttributes=null)

$data value: array of each $rowData from addRow()
$rowAttributes value: string or array of attributes for each row

Example of addRows()

Attributes

Attributes is a standard array for defining a cell or even a row, the keys are as follows:

key, value, col, row, skip, width, style

PhpSpreadsheet Original Usage Integration

This helper is flexible that you could inject or extract original PhpSpreadsheet with it, when you need to manipulate some Phpspreadsheet methods integrated with Helper.

Inject PhpSpreadsheet

…

Extract PhpSpreadsheet

php
use \yidas\phpSpreadsheet\Helper;

Helper::newSpreadsheet()
    ->setSheet(0, 'Sheet')
    ->addRow(['SN']);
    
// Get the PhpSpreadsheet object created by Helper
$objSpreadsheet = Helper::getSpreadsheet();
$objSpreadsheet->getProperties()
    ->setCreator("Nick Tsai")
    ->setTitle("Office 2007 XLSX Document");
    
// Get the actived sheet object created by Helper
$objSheet = Helper::getSheet();
$objSheet->setCellValue('A2', '1');
$objSheet->setCellValue('A3', '2');

Helper::output();

Merge Cells

It's easy to merge cells by defining each cell's span attributes:

  • row : Number of rowspan cells to merge with
  • col : Number of colspan cells to merge with
  • skip : Number of colspan cells to merge with
php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    ->addRows([
        [['value'=>'SN', 'row'=>2], ['value'=>'Language', 'col'=>2], ['value'=>'Block', 'row'=>2, 'col'=>2]],
        ['','English','繁體中文',['skip'=>2]],
    ])
    ->addRows([
        ['1', 'Computer','電腦','#15'],
        ['2', 'Phone','手機','#4','#62'],
    ])
    ->output('Merged Excel');

Multiple Sheets

setSheet()

Set an active PhpSpreadsheet Sheet

php
public static self setSheet($sheet=0, $title=NULL, $normalizeTitle=false)

getSheet()

Get PhpSpreadsheet Sheet object from cache

php
public static object getSheet($identity=null, $autoCreate=false)

Example:

…
  • getActiveSheetIndex(): Get active sheet index
  • getSheetCount(): Get sheet count

Map of Coordinates & Ranges

…

The result could be:

Array
(
    [sn] => A1
    [lang] => B1
    [block] => D1
    [lang-en] => B2
    [lang-zh] => C2
    [block-skip] => D2
)
Array
(
    [sn] => A1:A2
    [lang] => B1:C1
    [block] => D1:E2
    [lang-en] => B2:B2
    [lang-zh] => C2:C2
    [block-skip] => D2:E2
)
sn start cell: A1
sn start column: A
sn start row: 1
sn range: A1:A2
All range: A1:E4

Style Attributes

The style attribute could be set on a single cell, a single row or even a range of cells.

  • style: a attribute refers to applyFromArray() for styling
…

Style array key/value/constant refers Valid array keys for style applyFromArray() or Source Classes

Columns Format

The options for each cell data:

  • width: setWidth() for the column
php
\yidas\phpSpreadsheet\Helper::newSpreadsheet()
    ->addRow([['value'=>'ID', 'width'=>10], ['value'=>'Name', 'width'=>25], ['value'=>'Email', 'width'=>50]])
    ->addRows([
        ['1', 'Nick','[email protected]'],
        ['2', 'Eric','eric@.....'],
    ])
    ->output('My Excel'); 

All Cells Format

This section focuses on applying all actived cells or ranged cells on the sheet, not just effecting single cell, row or column.

setStyle()

Set Style for all actived cells or set by giving range to the actived sheet

php
public static self setStyle(array $styleArray, string $range=NULL)

Example:

…

setWrapText()

Set WrapText for all actived cells or set by giving range to the actived sheet

php
public static self setWrapText(string $range=NULL, string $value=true)

setAutoSize()

Set AutoSize for all actived cells or set by giving column range to the actived sheet

php
public static self setAutoSize(string $colAlphaStart=NULL, string $colAlphaEnd=NULL, boolean $value=true)

SETTING

setTimezone()

This helper will automatically align the spreadsheet’s timezone with your PHP default timezone.
However, you can still customize the target timezone for the spreadsheet if needed.

php
public static self setTimezone(string $timezoneString=NULL)

Example:

php
use \yidas\phpSpreadsheet\Helper;

Helper::setTimezone("Europe/London");
echo Helper::getTimezone();
$rows = $rowHelper::newSpreadsheet('/tmp/excel.xlsx')
    ->getRows();

LIMITATIONS

Performance Issue

If you're building large cell data with XLSX, you may face performance issue with memory usage and execution time.

box/spout spreadsheet lirary supports building Excel file with high performance, you could use this library instead if you do not need more style and formatting requirements.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPmultiple-sheetsphpphpexcelphpspreadsheet

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言