function api_import_user($app, $xls = '') { if (isset($_FILES) && isset($_FILES['excel_file'])) { $xls = $_FILES['excel_file']['tmp_name']; $result = importUserFromExcel($app, $xls, $_POST); @unlink($xls); $app->writeJSON($result); return; } $app->writeJSON(array('__raw' => true, 'error' => true, 'message' => 'Import Error.................'), 400, '400 Bad Request'); }
<?php $inputFileName = 'demo2.xls'; importUserFromExcel($inputFileName); function importUserFromExcel($inputFileName) { if (!class_exists('PHPExcel_IOFactory')) { require_once 'phar://' . __DIR__ . '/phpexcel.phar'; } // Read your Excel workbook 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(); $fields = array('user_id', 'title', 'first_name', 'last_name', 'email'); $visible_fields = array(); $result = array(); for ($row = 1; $row <= $highestRow; $row++) { $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); if (empty($visible_fields)) { foreach ($rowData[0] as $k => $v) { if (is_string($v)) { $v = trim($v); } if (in_array($v, $fields)) {