-
I'm trying to use fromArray() to setting a range of cells from array, however, it can can only set Array[0] into the sheet, but I need set all elements of this array into sheet. setReadDataOnly(TRUE); $spreadsheet = $reader->load("Customer Data.xlsx"); $worksheet = $spreadsheet->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); $CustomerA = Array(); for ($row = 2; $row <= $highestRow; $row++){ $customer = $worksheet -> getCellByColumnAndRow(1 , $row)->getValue(); if($customer == 'A'){ // Read a row of data into an array $rowData = $worksheet->rangeToArray('A'.$row . ':' . $highestColumn . $row, NULL, false, FALSE); if($CustomerA == null){ $CustomerA = $rowData; }else{ array_push($CustomerA,$rowData); } var_dump($CustomerA); echo ""; } } $newSpreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $newSpreadsheet->getActiveSheet() ->fromArray( $CustomerA, NULL, 'C3' ); $writer = new Xlsx($newSpreadsheet); $writer->save('A Customer.xlsx'); ?> |
Beta Was this translation helpful? Give feedback.
Answered by
oleibman
Aug 5, 2021
Replies: 1 comment 1 reply
-
First, you can make your code readable by placing if($CustomerA == null){
$CustomerA = $rowData;
} else {
array_push($CustomerA,$rowData);
} with the following line: $CustomerA[] = $rowData[0]; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Lydiachl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First, you can make your code readable by placing
``` php
before the code, and```
after.As for your problem, you can get the result you're looking for by replacing the following lines:
with the following line: