在 PHP Excel 中使用列名获取单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32526534/
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
Get Cell Value with column name in Php Excel
提问by user123
This is my excel sheet it has got lots of columns but i'm breaking down for ease in understanding the question
这是我的 excel 表,它有很多列,但为了便于理解问题,我将其分解
I'm reading Excel Sheet using PHP Exceland using rangeToArray() which give me all row from excel but i want the output as
我正在使用PHP Excel并使用 rangeToArray()读取 Excel Sheet ,它给出了 excel 中的所有行,但我希望输出为
Column as Key:Cell Value as Value
列作为键:单元格值作为值
Currently I'm get output as
目前我得到的输出为
Col Index :Cell Value
列索引:单元格值
So my Question is which is that function in Php Excel which return array with Column name and it Cell value?
所以我的问题是 Php Excel 中哪个函数返回带有列名和它的单元格值的数组?
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
for ($row = 2; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
printArr($rowData);
printArr("-------");
}
I get output as
我得到输出
Array
(
[0] => Array
(
[0] => 27745186
[1] => 42058
[2] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
...
...
)
[1] => Array
(
[0] => 27745186
[1] => 42058
[2] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
...
...
)
)
Desire Output
期望输出
Array
(
[0] => Array
(
[Invoice_no] => 27745186
[Invoice Date] => 42058
[Description] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
...
...
)
[1] => Array
(
[Invoice_no] => 27745186
[Invoice Date] => 42058
[Description] => DELL INTERNATIONAL SERVICES INDIA PVT LTD
...
...
)
)
回答by Mark Baker
There's nothing magic in PHPExcel, but it's pretty easy to do in standard PHP
PHPExcel 没有什么神奇之处,但在标准 PHP 中很容易做到
Retrieve the headings/keys that you want from the first row
从第一行检索您想要的标题/键
$headings = $sheet->rangeToArray('A1:' . $highestColumn . 1,
NULL,
TRUE,
FALSE);
Then reset the standard numeric keys to be the headings values inside your reading loop for each data row
然后将标准数字键重置为每个数据行的阅读循环内的标题值
for ($row = 2; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$rowData[0] = array_combine($headings[0], $rowData[0]);
}
回答by Ganesh
foreach ($cell_collection as $cell)
{
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();
if ($row == 1)
{
$header[$row][$column] = $data_value;
}
else
{
$arr_data[$row][$column] = $data_value;
}
}
回答by gabrielrincon
I create this function for don't to write the title of reference like A1, A2, B1, B2. And works.
我创建这个函数是为了不要像 A1、A2、B1、B2 那样写参考文献的标题。和作品。
public function createExcel($titulos, $datos, $tituloLibro = 'The title')
{
$phpExcelObject = //instance it;
$phpExcelObject->getProperties()->setCreator("Your name")
->setLastModifiedBy("yourweb.com")
->setTitle($tituloLibro)
->setSubject($tituloLibro)
->setDescription($tituloLibro)
->setKeywords("")
->setCategory($tituloLibro);
$i = 0;
$phpExcelObject->setActiveSheetIndex(0);
foreach ($titulos as $titulo)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,1)->setValue($titulo);
$i++;
}
$j = 2;
foreach ($datos as $filas)
{
$i = 0;
foreach ($filas as $fila)
{
$phpExcelObject->getActiveSheet()->getCellByColumnAndRow($i,$j)->setValue($fila);
$i++;
}
$j++;
}
// your logic
}