Responsive Advertisement

Import/Upload Excel file into MySQL using Codeigniter

On this codeigniter excel,csv import tutorial, we will would really like to share with you the way to import records as excel or csv report format in codeigniter. Excel and csv is the pleasant technique to import facts in a report and you may effortlessly import records to excel or csv using codeigniter excel library.

====>> Codeigniter Import Excel,CSV <<====

Contents.....

Download Codeigniter Latest
Basic Configurations
Download phpExcel Library
Create Library
Create Database With Table
Setup Database Credentials
Make New Controller
Create model
Create Views
Start Development server
Conclusion


Step 1:- First, we need to download  PHPExcel Library. Then extract PHPExcel Library inside “application/third_party” folder.

Download  PHPExcel Library

Step 2:- Create database and table or  copy below code and run in mysql for create table



CREATE TABLE `organization` (
  `id` int(10) NOT NULL,
  `org_name` varchar(150) NOT NULL,
  `org_code` int(50) NOT NULL,
  `gst_no` varchar(200) NOT NULL,
  `org_type` varchar(50) NOT NULL,
  `Address` varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Step 3:- Now create controller file Import.php and copy below code and paste in import.php file.

<?php 

defined('BASEPATH') OR exit('No direct script access allowed'); 
 
class Import extends CI_Controller { 

function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('import_model', 'import');
    }
     

  public function uploadCsv(){
   
    $this->load->view('upload');
  }
  public function uploadData(){

  if ($this->input->post('submit')) {
           
            $path = 'uploads/';
            require_once APPPATH . "/third_party/PHPExcel.php";
            $config['upload_path'] = $path;
            $config['allowed_types'] = 'xlsx|xls';
            $config['remove_spaces'] = TRUE;
            $this->load->library('upload', $config);
            $this->upload->initialize($config);           
            if (!$this->upload->do_upload('uploadFile')) {
                $error = array('error' => $this->upload->display_errors());
            } else {
                $data = array('upload_data' => $this->upload->data());
            }
            if(empty($error)){
              if (!empty($data['upload_data']['file_name'])) {
                $import_xls_file = $data['upload_data']['file_name'];
            } else {
                $import_xls_file = 0;
            }
            $inputFileName = $path . $import_xls_file;
           
            try {
                $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($inputFileName);
                $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
                $flag = true;
                $i=0;
                foreach ($allDataInSheet as $value) {
                  if($flag){
                    $flag =false;
                    continue;
                  }
                  $inserdata[$i]['org_name'] = $value['A'];
                  $inserdata[$i]['org_code'] = $value['B'];
                  $inserdata[$i]['gst_no'] = $value['C'];
                  $inserdata[$i]['org_type'] = $value['D'];
                  $inserdata[$i]['Address'] = $value['E'];
                  $i++;
                }             
                $result = $this->import->importdata($inserdata); 
                if($result){
                  echo "Imported successfully";
                }else{
                  echo "ERROR !";
                }           

          } catch (Exception $e) {
               die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                        . '": ' .$e->getMessage());
            }
          }else{
              echo $error['error'];
            }
           
           
    }
    $this->load->view('upload');
  }
}
?>



Step 4:- Now create import_model.php inside model folder and copy below code and paste in import_model.php


<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Import_model extends CI_Model {

    public function importData($data) {

        $res = $this->db->insert_batch('organization',$data);
        if($res){
            return TRUE;
        }else{
            return FALSE;
        }

    }

}


?>


Step 5:- Now create view file inside view folder and copy below code and paste in upload.php file

<form action="<?php echo base_url();?>form/uploadData" method="post" enctype="multipart/form-data">
    Upload excel file :
    <input type="file" name="uploadFile" value="" /><br><br>
    <input type="submit" name="submit" value="Upload" />
</form>



Note : You can use without Excelfile Library

Post a Comment

0 Comments