otzy/spreadsheet2array

从电子表格中提取表格到PHP数组

v1.0.3 2017-05-26 19:40 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:14:45 UTC


README

易于使用的PHPExcel包装器。如果您正在开发从电子表格导入的任何类型的应用程序,此库将帮助您将电子表格导入到PHP数组中。您可以从xls、xlsx、ods或其他PHPExcel支持的格式导入。

安装

composer require otzy/spreadsheet2array

如何使用

导入具有列名的表格


\Otzy\Spreadsheet2Array::readTable($file_name, $type = 'auto', $sheet = false, $first_row = 0, $first_col = 0,
                                                                         $col_names = false, $check_col_names = false)

是的,有很多参数,但使用非常简单。假设您有一个如下所示的电子表格(列和行标签未显示)

您的代码将是


$my_table = \Otzy\Spreadsheet2Array::readTable('path_to_your_file', 'auto', false, 0, 0, ['one', 'two', 'three']);

//$my_table will contain two dimensional array:
[
 ['one'=>1, 'two'=>2, 'three'=>3],
 ['one'=>1, 'two'=>2, 'three'=>3]
]

如果您不需要所有列,只需列出您需要的列


$my_table = \Otzy\Spreadsheet2Array::readTable('path_to_your_file', 'auto', false, 0, 0, ['one', 'three']);

//$my_table will contain two dimensional array:
[
 ['one'=>1, 'three'=>3],
 ['one'=>1, 'three'=>3]
]

始终检查所需字段名是否存在。如果至少缺少一个字段,则抛出Spreadsheet2ArrayException异常。

所有参数的完整描述
     * @param string $file_name name of file to read
     * @param string $type 'auto', 'csv', 'xls', 'xlsx', 'ods'
     *
     * @param bool|string|int $sheet Sheet Name/Index to read.
     * Index must be passed as an integer. Sheets are zero-based. I.e. first sheet has index=0
     * If false read active sheet. This is normally the sheet, that was active at the moment of Save in Excel or Open Office
     * Not applicable for csv
     *
     * @param int $first_row first row to read (zero based).
     *
     * @param int $first_col first column to read (zero based).
     *
     * @param string[]|bool $col_names array of field names or false to use $firstRow for field names
     *
     * @param bool $check_col_names Parameter works only when $col_names !== false.
     * If true - check that column names and the order of names are exactly the same as value and order of cells in the first row.<br>
     * If false - only check that all $col_names are represented in the first row
     *