5
$eanStyle = new \PHPExcel_Style();
$eanStyle->getNumberFormat()->applyFromArray([
    'code' => '0000000000000'
]);

/* apply styles */
$mainSheet->duplicateStyle($eanStyle, 'A2:A10000');

Code above generates .xlsx template file, user enters data (7 rows) and upload file and then:

$mainSheet->getHighestRow('A'); //  retruns 10000 instead of 8 (7 rows + header)

Thanks in advance for help.

Maytyn
  • 122
  • 1
  • 10

1 Answers1

0

I would advise you create a read filter to read only specific rows and columns. This would prevent the other empty rows being included:

$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #3';

/**  Define a Read Filter class implementing \PhpOffice\PhpSpreadsheet\Reader\IReadFilter  */
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
    public function readCell($column, $row, $worksheetName = '') {
        //  Read rows 1 to 7 and columns A to E only
        if ($row >= 1 && $row <= 7) {
            if (in_array($column,range('A','E'))) {
                return true;
            }
        }
        return false;
    }
}

/**  Create an Instance of our Read Filter  **/
$filterSubset = new MyReadFilter();

/**  Create a new Reader of the type defined in $inputFileType  **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/**  Tell the Reader that we want to use the Read Filter  **/
$reader->setReadFilter($filterSubset);
/**  Load only the rows and columns that match our filter to Spreadsheet  **/
$spreadsheet = $reader->load($inputFileName);
Barry
  • 3,303
  • 7
  • 23
  • 42
  • 1
    Thanks for answear but as I assume it's kind of 'static', `7 rows` was an example. Number of rows is unfortunately unknown. I've tried use `getHighestDataRow()` and result is the same. – Maytyn Oct 18 '18 at 11:30
  • 1
    @Maytyn it was a shot in the dark... there are other read filters that might help but I can see what you are saying... hope someone else has a better answer for you – Barry Oct 18 '18 at 11:34
  • @Maytyn Would this https://phpspreadsheet.readthedocs.io/en/stable/topics/accessing-cells/#looping-through-cells can help you? – MSI Nov 06 '18 at 12:35