Created
July 6, 2018 14:14
-
-
Save viramgamijignesh/6734c5a20b3f602856e929135fd070d4 to your computer and use it in GitHub Desktop.
CI excel read data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!class_exists('PHPExcel')) return false; | |
$directory = "./excel/"; | |
$files = glob($directory . "*"); | |
foreach ($files as $filesName) | |
{ | |
$inputFileName = $filesName; | |
$row_callback=null; | |
try { | |
$inputFileType = PHPExcel_IOFactory::identify($inputFileName); | |
$objReader = PHPExcel_IOFactory::createReader($inputFileType); | |
$objPHPExcel = $objReader->load($inputFileName); | |
} catch(Exception $e) { | |
return ('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); | |
} | |
$sheet = $objPHPExcel->getSheet(0); | |
$highestRow = $sheet->getHighestRow(); | |
$highestColumn = $sheet->getHighestColumn(); | |
$keys = array(); | |
$results = array(); | |
if(is_callable($row_callback)){ | |
for ($row = 1; $row <= $highestRow; $row++){ | |
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,null,true,false); | |
if ($row === 1){ | |
$keys = $rowData[0]; | |
} else { | |
$record = array(); | |
foreach($rowData[0] as $pos=>$value) $record[$keys[$pos]] = $value; | |
$row_callback($record); | |
} | |
} | |
} else { | |
for ($row = 1; $row <= $highestRow; $row++){ | |
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,null,true,false); | |
if ($row === 1){ | |
$keys = $rowData[0]; | |
} else { | |
$record = array(); | |
foreach($rowData[0] as $pos=>$value) | |
$record[$keys[$pos]] = $value; | |
$results[] = $record; | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment