20

I'm using akeneo-labs spreadsheet-parser library to extract data from xlsx file.

use Akeneo\Component\SpreadsheetParser\SpreadsheetParser;

$workbook = SpreadsheetParser::open('myfile.xlsx');

$myWorksheetIndex = $workbook->getWorksheetIndex('myworksheet');

foreach ($workbook->createRowIterator($myWorksheetIndex) as $rowIndex => $values) {
    var_dump($rowIndex, $values);
}

Actually, you can get value by column index in a loop, is it possible to get value by column name instead?

Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
Zeta
  • 663
  • 1
  • 12
  • 27

3 Answers3

0

maybe using another package as suggested fixes your problem. also you can use array_column https://www.php.net/manual/en/function.array-column.php

Moaaz
  • 21
  • 2
0

Maybe you can do that in a spreadsheet CSV file by using PHP default function fgetcsv(), you can go throw an overview from here: https://www.php.net/manual/en/function.fgetcsv fgetcsv — Gets line from file pointer and parse for CSV fields

First of all, save as your Xls file in CSV type then you can take your value from that CSV file by column name.

Nayana Chandran
  • 1,416
  • 1
  • 16
  • 30
0

You can try this.

use Akeneo\Component\SpreadsheetParser\SpreadsheetParser;

$workbook = SpreadsheetParser::open('myfile.xlsx');

$myWorksheetIndex = $workbook->getWorksheetIndex('myworksheet');
// all columns
$columns = [];

foreach ($workbook->createRowIterator($myWorksheetIndex) as $rowIndex => $values) {
    if ($rowIndex == 1) {
        $columns = $values;
    } else {
        $datum = array_combine($columns, $values);
        // get value by column name
        var_dump($datum['name']);
    }
}
Plantour
  • 96
  • 5