I\'m trying to use PHPExcel with CodeIgniter.
My problem is when i want to use this method below, I got PHP Fatal Error : Cannot redeclare class IOFactory
all
I am using PHPExcel to upload data from Excel in Mysql with CI and grocery_CRUD. This is my way:
// My Excel CI Lib
//My Excel CI Model
load->library('Lib_excel', null, 'excel');
// load excel file
$objPHPExcel = $this->excel->load($excelFileName);
// get active sheets
$objWorksheet = $objPHPExcel->getActiveSheet();
// get highest row column
$lastRow = $objWorksheet->getHighestRow();
// check cell value empty or not
for ($i = $startRow; $i < $lastRow; $i++) {
$colB = $objPHPExcel->getActiveSheet()->getCell($columnName . $i)->getValue();
if ($colB == NULL || $colB == '') {
die('The Cell' . ' B' . $i . ' is empty. Please, remove or fill with data');
}
}
// get excel data with range e.g. (B2:B56)
$cellValues = $objPHPExcel->getActiveSheet()->rangeToArray("$columnName$startRow:$columnName$lastRow");
// return all data as array
return $cellValues;
}
}
?>
// My Controller to call method
load->model('Mod_excel', 'ModExcel', true);
// Load CRUD lib
$this->load->library('grocery_CRUD');
}
/**
* Upload Coupon Code from Excel
*/
public function createCouponCode() {
$crud = new grocery_CRUD();
$crud->set_table('coupon_code');
$crud->fields('coupon_no');
$crud->columns('coupon_no');
$crud->set_subject('Coupon');
$crud->required_fields('coupon_no');
$crud->set_field_upload('coupon_no', '../public/uploads/');
$crud->callback_before_upload(array(
$this,
'_excel_uploaded_file'
));
$output = $crud->render();
$this->_example_output($output);
}
/**
* Call back Function form CRUD Upload
* @param array $files_to_upload
* @param array $field_info
* @return string
*/
function _excel_uploaded_file($files_to_upload = array(), $field_info = array()) {
$insert_data = array();
$ext = '';
$file_tmp_path = '';
foreach ($files_to_upload as $value) {
$ext = pathinfo($value['name'], PATHINFO_EXTENSION);
$file_tmp_path = $value['tmp_name'];
}
$allowed_formats = array(
"xlsx"
);
if (in_array($ext, $allowed_formats)) {
$excelFileName = $file_tmp_path;
$columnName = 'B';
$startRow = '2';
$cellValues = $this->ModExcel->readWithExcel($excelFileName, $columnName, $startRow);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($cellValues));
foreach ($iterator as $value) {
$insert_data[] = array(
'coupon_no' => $value
);
}
$this->output->enable_profiler(TRUE); //Turns on CI debugging
$this->db->insert_batch('coupon_code', $insert_data);
echo ($status) ? true : false;
} else {
return 'Error: Wrong file format. Use Excel 2007 format';
}
}
}
?>
// My Folder where I put PHPExcel Lib in CI