Thursday, 25 April 2013

Insert Excel sheet data in mysql dataase using php

First download excel reader.php file

http://code.google.com/p/php-excel-reader/downloads/detail?name=php-excel-reader.zip&can=2&q=


Code 
______

 require_once("config.php");
require_once 'excel_reader_2.php';


    $excelFileName = $_FILES['img_upload']['name'];
    if($excelFileName != "")
    {
   

        $uploadDir = '../uploadexcelfiles/';
        $uploadFile = $uploadDir . $excelFileName ;
        $filename=$_FILES['img_upload']['name'];
        $ext = substr($filename, strpos($filename,'.'), strlen($filename));
   
        if($ext == '.xls')
        {
           
            if (move_uploaded_file($_FILES['img_upload']['tmp_name'], $uploadFile))
            {

                $data = new Spreadsheet_Excel_Reader();
                $data->setOutputEncoding('CP1251');
                $data->read($uploadFile);
                $flag=false;
                $rows=$data->sheets[0]['numRows'];
                $rows -=1;


                    for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++)
                    {
                        $age="";
                        $wins="";
                        $most="";
                        $year="";
   
                        $sql = false;
                        for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
                        {
                            if (isset($data->sheets[0]['cells'][$i][$j]) && $data->sheets[0]['cells'][$i][$j] == 'Age')
                            {
                                $flag = true;
                                $i++;
                            }
                            if (isset($flag))
                            {
                                if (isset($data->sheets[0]['cells'][$i][$j]) &&  $data->sheets[0]['cells'][$i][$j] != "")
                                {
                                    if ($j == 1)
                                    {   
                                        $age = $data->sheets[0]['cells'][$i][$j];
                                        $age = !empty($age) ? $age : null;
                                        $sql = true;
                                    }
                                    if ($j == 2)
                                    {
                                        $wins =  $data->sheets[0]['cells'][$i][$j];
                                        $wins = !empty($wins) ? $wins : null;
                                        $sql = true;
                                    }
                                    if ($j == 3)
                                    {
                                        $most =  $data->sheets[0]['cells'][$i][$j];
                                        $most = !empty($most) ? $most : null;
                                        $sql = true;
                                    }
       
                                    if ($j == 4)
                                    {
                                        $year =  $data->sheets[0]['cells'][$i][$j];
                                        $year = !empty($year) ? $year : null;
                                        $sql = true;
                                    }
       
       
                                }
                            }//end flag if
                        }//end j if
                                if (isset($sql))
                                {
                                    $sSql = 'insert into group_age_tbl ( age, wins, most, year, group_id) values("'. $age . '","'.$wins.'","'. $most . '","'.$year.'","'.$group_id.'")';
                                            $res=execute($sSql);
                                            $inserted=mysql_insert_id();       
                                            $arrayid[] = $inserted;
                                }//end if of SQL
                        }
                }//end move_uploaded_file
             if($arrayid != "")
                $count=count($arrayid);
            else
                $count=0;
       } //end $ext == '.xls'
}//$excelFileName

$file=$_FILES['img_upload']['name'];
$path=$localpath ."uploadexcelfiles/". $file;
if(file_exists($path))
{
    unlink($path);
}
header("location:destination.php");
exit();



No comments:

Post a Comment