0

I have written code to import data from excel sheet and for that I want to count total number of rows and columns.

I am using this SpreadsheetReader library.

Here is my code:

$uploadFilePath = 'uploads/'.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);
$Reader = new SpreadsheetReader($uploadFilePath);
$Sheets = $Reader -> Sheets();
foreach ($Sheets as $Index => $Name)
{
    $Reader -> ChangeSheet($Index);
    foreach ($Reader as $Key => $Row){
        for($i=0; $i<=TOTALCOLUMNS; $i++){//I need to count total columns here
            if(!empty($Row[$i])){
                if($i==0)
                    $parent=$Row[$i];
                else{
                    if(!empty($Row[(int)$i-1]))
                        $parent=$Row[(int)$i-1];
                }
                $category=$Row[$i];  
                $data['category']=$category;             
                $data['parent']=$parent;             
                $res=saveCategory($data);
            }                
        }
    }
    echo "<br>Categories imported successfully<br>";
}
Shreya
  • 1
  • 1

2 Answers2

0

You can use:

$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
-1
$TOTALCOLUMNS = sizeof($Row)        // count total columns here

sizeof($Row) count total of columns from SpreadsheetReader

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Please add a comment on why your solution will work. This isn't enough to be understood by the author of this post. – D. Lawrence Jan 03 '20 at 10:47