How to import excel, read data from excel file using a PHPExcel in Codeigniter

In this tutorial, we will learn how to import or read an Excel sheet of data and insert it into the database using PHPExcel in Codeigniter Library. For that, we will create a Database to insert excel data into a table, a form to make a view to upload an excel sheet, and also create a PHP script to perform operations to insert data into Database. Import Excel into MySQL helps to save the user time and avoid repetitive work. Here is the use of PHPExcel in Codeigniter

require_once APPPATH.'third_party/PHPExcel.php';
$this->excel = new PHPExcel(); 

import excel file into database in codeigniter.
First Step : Insert data in table using PHPExcel Library in Controller.

$file_info = pathinfo($_FILES["result_file"]["name"]);
$file_directory = "uploads/";
$new_file_name = date("d-m-Y ") . rand(000000, 999999) .".". $file_info["extension"];

if(move_uploaded_file($_FILES["result_file"]["tmp_name"], $file_directory . $new_file_name))
{   
    $file_type	= PHPExcel_IOFactory::identify($file_directory . $new_file_name);
    $objReader	= PHPExcel_IOFactory::createReader($file_type);
    $objPHPExcel = $objReader->load($file_directory . $new_file_name);
    $sheet_data	= $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
    foreach($sheet_data as $data)
    {
        $result = array(
                'Loat_NO' => $data['A'],
                'C_Name' => $this->input->post('name'),
                'Lab' => $data['V'],
                'C_Shape' => $shape,
                'C_Carat' => $data['C'],
                'C_Weight' => $data['C'],
                'C_Color' => $C_Color,
                'C_Clarity' => $C_Clarity,
                'C_Rap' => $data['F'],
                'C_Discount' => $supplierdiscount,
                'C_Rate' => ($data['H'])?$data['H']:'',
                'C_NetD' => $data['I'],
                'C_DefthP' => $data['S'],
                'C_TableP' => $data['T'],
                'C_Cut' => $C_Cut,
                'C_Polish' => $C_Polish,
                'C_Symmetry' => $C_Symmetry,
                'C_Fluorescence' => $C_Fluorescence,
                'Milky' => ($data['P'])?$data['P']:'',
                'Certi_NO' => $data['W'],
                'Key_Symbols' => $data['X'],
                'C_Length' => $C_Length,
                'C_Width' => $C_Width,
                'C_Depth' => $C_Depth,
                'Location' => '16',
                'is_delete' => '0',
        );
        $this->Uploaddiamond_Model->postDiamond($result);
    }
}

Now Call model $this->Uploaddiamond_Model->postDiamond($result);

function postDiamond($result)
{
    $this->db->insert('diamond_master',$result);
    return true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *