php PHPExcel 如何仅获取 1 个单元格值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22566638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:06:15  来源:igfitidea点击:

PHPExcel How to get only 1 cell value?

phpphpexcel

提问by CrandellWS

I would think that a getCell($X, $y)or getCellValue($X, $y)would be available for one to easily pick a a certain value. This can be usefully, as example crosscheck data prior to a larger process.

我认为 a getCell($X, $y)orgetCellValue($X, $y)可供人们轻松选择某个值。这可能很有用,例如在更大的过程之前交叉检查数据。

How do you get a specific value from say cell C3.

你如何从单元格C3 中获得特定值。

I do not want an array of values to sort through.

我不希望对一组值进行排序。

回答by Mark Baker

Section 4.5.2 of the developer documentation

开发者文档的第 4.5.2 节

Retrieving a cell by coordinate

通过坐标检索单元格

To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCell method. A cell's value can be read again using the following line of code:

要检索单元格的值,应首先使用 getCell 方法从工作表中检索单元格。可以使用以下代码行再次读取单元格的值:

$objPHPExcel->getActiveSheet()->getCell('B8')->getValue();


Section 4.5.4 of the developer documentation

开发者文档第 4.5.4 节

Retrieving a cell by column and row

按列和行检索单元格

To retrieve the value of a cell, the cell should first be retrieved from the worksheet using the getCellByColumnAndRow method. A cell's value can be read again using the following line of code:

要检索单元格的值,应首先使用 getCellByColumnAndRow 方法从工作表中检索单元格。可以使用以下代码行再次读取单元格的值:

// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getValue();

If you need the calculated value of a cell, use the following code. This is further explained in 4.4.35

如果需要计算单元格的值,请使用以下代码。这在 4.4.35 中进一步解释

// Get cell B8
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, 8)->getCalculatedValue();

回答by Brian North

By far the simplest - and it uses normal Excel co-ordinates:

到目前为止最简单的 - 它使用普通的 Excel 坐标:

// Assuming $sheet is a PHPExcel_Worksheet

$value = $sheet->getCell( 'A1' )->getValue();

You can separate the co-ordinates out in a function if you like:

如果您愿意,您可以在函数中分离坐标:

function getCell( PHPExcel_Worksheet $sheet, /* string */ $x = 'A', /* int */ $y = 1 ) {

    return $sheet->getCell( $x . $y );

}

// eg:
getCell( $sheet, 'B', 2 )->getValue();

回答by CrandellWS

This is a source based answer feel free to improve or comment.

这是一个基于源的答案,随时改进或评论。

function toNumber($dest)
{
    if ($dest)
        return ord(strtolower($dest)) - 96;
    else
        return 0;
}

function myFunction($s,$x,$y){
 $x = toNumber($x);
 return $s->getCellByColumnAndRow($x, $y)->getFormattedValue();
}


$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->setActiveSheetIndex(0);
$sheetData = $objPHPExcel->getActiveSheet();


$cellData = myFunction($sheetData,'B','2');
var_dump($cellData);

This does not work past the letter Z, and could be improved but works for my needs.

这在字母 Z 之后不起作用,可以改进但可以满足我的需要。