mehrshaddarzi
10/9/2017 - 3:27 PM

Load Excel file in php with phpexcel

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;