This is my excel sheet it has got lots of columns but i'm breaking down for ease in understanding the question
I'm reading Excel Sheet using PHP Excel and using rangeToArray() which give me all row from excel but i want the output as
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?
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
...
...
)
)
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 using the getValue() method. // Get the value from cell A1 $cellValue = $spreadsheet->getActiveSheet()->getCell('A1')->getValue();
Export Data to Excel with PHP The $data variable holds the data in array format which will be exported to Excel using PHP. ); The filterData() function is used to filter string before added to the excel sheet row. The following code helps to export data in excel and download it as a file.
There's nothing magic in PHPExcel, but it's pretty easy to do in standard 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]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With