在Php Excel中使用列名获取单元格值

时间:2015-09-11 15:02:27

标签: php phpexcel

这是我的excel表Excel Sheet它有很多专栏,但我为了理解这个问题而分手了

我正在使用PHP Excel阅读Excel Sheet并使用rangeToArray()从excel中获取所有行但我希望输出为

  

列为键:单元格值为值

目前我的输出为

  

Col Index:Cell Value

所以我的问题是Php Excel中的哪个函数返回带有列名的数组并且它是Cell值?

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("-------");

}

我输出为

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
            ...
            ...
         )
)

欲望输出

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
            ...
            ...
         )
)

3 个答案:

答案 0 :(得分:11)

PHPExcel中没有任何魔力,但在标准PHP中很容易做到

从第一行中检索所需的标题/键

$headings = $sheet->rangeToArray('A1:' . $highestColumn . 1,
                                            NULL,
                                            TRUE,
                                            FALSE);

然后将标准数字键重置为每个数据行的读取循环内的标题值

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]);
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我创建此功能是为了不写出参考标题,如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

}