[英]Get value from a certain row in a column
I currently have the below code that will loop through the excel file and return rows with missing data. 我目前有以下代码,这些代码将循环浏览excel文件并返回缺少数据的行。 the output is as below: 输出如下:
- Row number: 37
- Missing : in cell B37
- Missing : in cell D37
What I would like to return is the following 我想返回的是以下内容
- Row number: 37
- Missing nameFirst : in cell B37
- Missing nameLast : in cell D37
Where nameFirst is the value of cell B1 and nameLast is the value of D1. 其中nameFirst是单元格B1的值,nameLast是D1的值。
<?PHP
require_once 'Excel/PHPExcel.php';
require_once 'Excel/PHPExcel/Reader/Excel2007.php';
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
$Retour=false;
$column=PHPExcel_Cell::columnIndexFromString($column);// Warning ! A=1, not zero as usual
if($row<1 || $column>4)
$Retour=false;
else
$Retour=true;
return $Retour;
}
}
echo '<pre>';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load('test.xlsx');
$worksheet=$objPHPExcel->getSheet(0);
foreach ($worksheet->getRowIterator() as $row) {
if($row->getRowIndex()>1){
echo ' - Row number: ' . $row->getRowIndex() . "\r\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
$colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
if($cell->getCalculatedValue() == "")
echo ' - Missing <THIS IS WHERE I WANT THE INDEX CELL FOR THE COLUMN> :' . 'in cell ' . $cell->getCoordinate() . "\r\n";
}
}
}
echo '</pre>';
?>
I tried changing the echo to include the variable $colIndex in addition to: 我尝试更改回显,除了包含变量$ colIndex之外:
echo ' - Missing '. $worksheet->getCellByColumnAndRow($colIndex, 1)->getValue(). '. :' . $cell->getCoordinate() . "\r\n";
But this causes a return of one row and only one correct cell and a whole bunch of blank variables 但这会导致返回一行,仅返回一个正确的单元格和一堆空白变量
- Row number: 2
- Missing nameFirst. :B1
- Missing . :E1
- Missing . :F1
- Missing . :G1
- Missing . :H1
So you need to get the cell value for row 1 in the current cell's ($cell) column 因此,您需要获取当前单元格($ cell)列中第1行的单元格值
echo ' - Missing ' .
$worksheet->getCell($cell->getColumn(). '1')->getValue() .
' :' . 'in cell ' .
$cell->getCoordinate() . "\r\n";
should work perfectly well 应该可以很好地工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.