tiempor3al / excelreader
简单快捷的 Excel .xlsx 解析器
This package is not auto-updated.
Last update: 2024-09-22 14:56:55 UTC
README
ExcelReader 是一个非常简单快捷且没有第三方依赖的 Excel (.xlsx) 解析器。
<?php require __DIR__ . '/vendor/autoload.php'; use tiempor3al\Excel\Reader as ExcelReader; if(!isset($argv[1])){ die("Missing Excel filename..."); } $reader = new ExcelReader(); $reader->Initialize($argv[1]); $worksheets = $reader->getWorksheets(); //We parse the first worksheet and the first two columns $generator = $reader->ParseWorksheet($worksheets[0], ['A:B']); //Since the library uses generators, it consumes little memory foreach ($generator as $cell) { if ($cell === ExcelReader::START_ROW) { //Do initialization stuff here printf("Start of row...\r\n"); } //if cell is an array then we get its value if (is_array($cell)) { //$cell holds four values: //'cell' => A8 - the position of the cell //'row' => 8 - the row of the cell //'col' => A - the column of the cell //'value' => xyz - the value of the cell //For values like strings and numbers, we can get the value in $cell['value'] if ($cell['col'] == 'A') { printf("Cell value: %s\r\n",$cell['value']); } //For dates we need to convert the values using the toDate method //Suppose that column B is filled with dates if ($cell['col'] == 'B') { //toDate returns a DateTime Object $date = $reader->toDate($cell['value']); printf("Cell value: %s\r\n",$date->format('Y-m-d')); } } if ($cell === ExcelReader::END_ROW) { printf("End of row...\r\n"); } }