Skip to content

Instantly share code, notes, and snippets.

@mehrshaddarzi
Created October 9, 2017 15:27
Show Gist options
  • Save mehrshaddarzi/b463fcb5c2399ba3f5da2d5fe7fc95a8 to your computer and use it in GitHub Desktop.
Save mehrshaddarzi/b463fcb5c2399ba3f5da2d5fe7fc95a8 to your computer and use it in GitHub Desktop.
Load Excel file in php with phpexcel
require_once str_replace("\\","/",get_template_directory()) . '/includes/phpexcel/PHPExcel.php';
// Create new PHPExcel object
//$objPHPExcel = new PHPExcel();
// Create new PHPExcel object
$upload_dir = str_replace("\\","/",wp_upload_dir()['basedir']);
try {
$objPHPExcel = PHPExcel_IOFactory::load($upload_dir.'/t.xlsx');
$dataArr = array();
foreach ( $objPHPExcel->getWorksheetIterator() as $worksheet ) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString( $highestColumn );
for ( $row = 1; $row <= $highestRow; ++ $row ) {
for ( $col = 0; $col < $highestColumnIndex; ++ $col ) {
$cell = $worksheet->getCellByColumnAndRow( $col, $row );
$val = $cell->getValue();
$dataArr[ $row ][ $col ] = $val;
}
}
}
} catch (ArithmeticError | Exception $e) {
echo 'lhata';
}
echo '<pre>';
print_r($dataArr);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment