Get cell value with column name in php excel

This is my excel sheet Excel sheet, it has a lot of columns but i'm upset to understand the question

I am reading an Excel worksheet using PHP Excel and using rangeToArray () which give me the whole row from excel, but I want the result to be like

Column as key: cell value as value

I am currently getting output as

Col index: cell value

So my question is, is this a function in Php Excel that returns an array with column name and cell value?

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

Desired 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
            ...
            ...
         )
)
+4
source share
3 answers

PHPExcel , PHP

/

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

reset

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

I create this function so as not to write a link title such as A1, A2, B1, B2. And working.

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

}
0
source

Source: https://habr.com/ru/post/1606977/


All Articles