2

现在我正在尝试查找如何在laravel excel中获取当前行号或单元格 ID,但我找不到它,我需要获取当前行号或单元格 ID,因为我需要生成一些提及当前行号的错误消息或单元格编号。

excel_file_image,

请打开 excel 图像链接以查看图像,例如,根据图像我想知道当前的活动单元格,例如活动单元格位于 B 列和第 2 行,我如何获得该单元格索引或行号?

$sheet = 'public/site.xls';

        // load excel content
        $excel = Excel::load($sheet, function ($reader)
        {
            $results = $reader->toObject();

            foreach($results as $result)
            {

               // i want to know how to retrieve active cell index here

            }

        });

注意:我不想要单元格的值,但我想要单元格的索引

4

1 回答 1

1

在我的情况下,我希望用户在上传 excel 文件时指定包含电话号码的列号。因此,我需要按用户输入的索引号查找列。

在您和我的情况下,我们都需要行索引和列索引。这是我破解它的方法。 在此处输入图像描述

//Array to hold the phone numbers
$phone_numbers = array();

//Load Excel file as array
$rows = Excel::load($contacts_file)->toArray();

//Loop through each row
foreach($rows as $row_index=>$row){ //$row_index is the row index
    $col_headers = array_keys($row); //Gives you an array, in my case ["serial","contact_name","contact_phone"]
    //Getting Cell Value i.e phone number
    array_push($phone_numbers,$row[$col_headers[2]]); //$col_headers[2] is the column index for phone
}
return $phone_numbers;    
于 2017-06-01T08:37:59.310 回答