计算来自SpreadsheetReader的行和列总数

时间:2019-07-15 11:41:47

标签: php import-from-excel phpspreadsheet

我已经编写了从Excel工作表导入数据的代码,为此,我想计算行和列的总数。

我正在使用此SpreadsheetReader library

这是我的代码:

$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>";
}

2 个答案:

答案 0 :(得分:0)

您可以使用:

$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'

答案 1 :(得分:-1)

$ TOTALCOLUMNS = sizeof($ Row)//在此处计算总列数

// sizeof($ Row)计算SpreadsheetReader中的列总数