Bladephp: Now with 7+ codeigniter, php, and laravel project 7+ php project From $18/Year From Rs. 1000/Year

PHPExcel import excel to mysql in Codeigniter

by
Category:codeigniter
codeigniter mysql phpexcel
PHPExcel import excel to mysql in Codeigniter

In this tutorial, we will learn how to import an Excel sheet into database using PHPExcel Library. For that, we will create a Database to insert an excel data into table, a form to make a view to upload an excel sheet and also create a PHP script to perform operation to insert data into Database.

Install PHPExcel Library in Codeigniter.

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

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;
}

1 Comments

Rodolfo Saavedra

07 Mar 2018

thanks

Html code work in comment box


Related Post