Date Posted:14-06-2017
In this post  we will explain codeigniter importing data using excel file and PHPExcel libary
I assume that you are configure or setup the codeigniter

step1:Download PHPExcel libarry from it’s github repository or you can download it from PHPExcel official website.
Dwonload PHPExcel library
Dwonload PHPExcel library from GIThub repository from https://phpexcel.codeplex.com/

Step2:

After download you find three differnt folder Classes,Documentation,Examples in PHPExcel.navigate to application/thirdparty extract PHPExcel->Classes->PHPExcel.php and PHPExcel directory directory here.

Step3:
Navigate to application/libraries and create new file name as ExcelImport_lib.php,place following code here

<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
 ini_set('error_reporting', E_STRICT); 
 require_once APPPATH."/third_party/PHPExcel.php"; 
 class ExcelImport_lib extends PHPExcel 
 { public function __construct() 
 	{ parent::__construct(); } 
 } ?>

Step4:
Navigate to application/controllers and create new file name as ImportExcel.php,place following code here

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/**
 * summary
 */
class ImportExcel extends CI_Controller
{
    public function index()
    {
    	$this->load->view('excelimport');	
    }
    public function Import($value='')
    {
        $this->load->library('ExcelImport_lib');
 			
     try {
    /// it will be your file name that you are posting with a form or can pass static name $_FILES["file"]["name"];
     	// $objPHPExcel = PHPExcel_IOFactory::load('uploads/'.$_FILES["file"]["name"]);
     	$objPHPExcel = PHPExcel_IOFactory::load('uploads/'.'yourTableName.xls');

         }
     catch(Exception $e)
        {
           $this->resp->success = FALSE;
           $this->resp->msg = 'Error Uploading file';
           echo json_encode($this->resp);
           exit;
        }

     $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
     print_r($allDataInSheet);
    foreach($allDataInSheet as $import)
    {
      echo $import['A']; /// will return values of Col A
      echo $import['B']; /// will return values of Col B
      echo $import['C']; /// will return values of Col C
      echo $import['D']; /// will return values of Col D

    }
    }
}

?>

Step5:
Navigate to application/view and create new file name as excelimport.php,place following code here

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Welcome to CodeIgniter</title>

  <style type="text/css">

  ::selection { background-color: #E13300; color: white; }
  ::-moz-selection { background-color: #E13300; color: white; }

  body {
    background-color: #fff;
    margin: 40px;
    font: 13px/20px normal Helvetica, Arial, sans-serif;
    color: #4F5155;
  }

  a {
    color: #003399;
    background-color: transparent;
    font-weight: normal;
  }

  h1 {
    color: #444;
    background-color: transparent;
    border-bottom: 1px solid #D0D0D0;
    font-size: 19px;
    font-weight: normal;
    margin: 0 0 14px 0;
    padding: 14px 15px 10px 15px;
  }

  code {
    font-family: Consolas, Monaco, Courier New, Courier, monospace;
    font-size: 12px;
    background-color: #f9f9f9;
    border: 1px solid #D0D0D0;
    color: #002166;
    display: block;
    margin: 14px 0 14px 0;
    padding: 12px 10px 12px 10px;
  }

  #body {
    margin: 0 15px 0 15px;
  }

  p.footer {
    text-align: right;
    font-size: 11px;
    border-top: 1px solid #D0D0D0;
    line-height: 32px;
    padding: 0 10px 0 10px;
    margin: 20px 0 0 0;
  }

  #container {
    margin: 10px;
    border: 1px solid #D0D0D0;
    box-shadow: 0 0 8px #D0D0D0;
  }
  </style>
</head>
<body>

Welcome to CodeIgniter!

" enctype="multipart/form-data" method="POST" role="form"> Form title
</body> </html>

 

Leave a Reply