In this i have used Jphpexcel reader to read excel and save in to the database. All the codes i got from another topic. thanking him i added some more lines to the controller.
$model=new XUploadForm;
if(isset($_POST['excelsubmit']))
{
$rnd = rand(0, 9999);
// generate random number between 0-9999
$uploadedFile = CUploadedFile::getInstance($model, 'file');
$fileName = "{$rnd}-{$uploadedFile}";
// random number + file name
//$ext=substr(strrchr($model->name,'.'),1);
$uploadedFile -> saveAs('uploads/' . $fileName);
chmod( 'uploads/' . $fileName, 0777 );
//using php excel
Yii::import('ext.phpexcelreader.JPhpExcelReader');
$data=new JPhpExcelReader('uploads/' . $fileName);
//print_r($data);
//$datas = CHtml::listData($data, 'id', 'product_name');
//print_r($data);
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++)
{
// check if item number is empty
if (empty($data->sheets[0]['cells'][$i][1]))
continue;
//Getting all the excel data:++++++++++++++++++++++++++++++
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++)
{
$content[$j] = $data->sheets[0]['cells'][$i][$j];
//echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
$category_id=$data->sheets[0]['cells'][$i][2];
$products_id=$data->sheets[0]['cells'][$i][3];
$products_code=$data->sheets[0]['cells'][$i][4];
$stores_id=$data->sheets[0]['cells'][$i][5];
$price=$data->sheets[0]['cells'][$i][6];
$deal_id=$data->sheets[0]['cells'][$i][7];
$currency=$data->sheets[0]['cells'][$i][8];
$product_url=$data->sheets[0]['cells'][$i][9];
$title=$data->sheets[0]['cells'][$i][10];
$description=$data->sheets[0]['cells'][$i][11];
$expiry_date=$data->sheets[0]['cells'][$i][12];
}
$command = "Select * from ProductStoreMap where products_id='$products_id' and stores_id='$stores_id'";
$result = Yii::app()->db->createCommand($command)->query();
$num=$result->getRowCount();
if($num==0){
//checkprod
Yii::app()->db->createCommand(" INSERT INTO ProductStoreMap(category_id ,products_id,products_code,stores_id,price,deal_id,currency,product_url) VALUES('$category_id' ,'$products_id','$products_code','$stores_id','$price','$deal_id','$currency','$product_url') ")->execute();
Yii::app()->db->createCommand(" INSERT INTO deal(title,description,expiry_date ) VALUES('$title','$description','$expiry_date') ")->execute();
}else{
Yii::app()->db->createCommand(" update ProductStoreMap set category_id='$category_id' ,products_id='$products_id',products_code='$products_code',stores_id='$stores_id',price='$price',deal_id='$deal_id',currency='$currency',product_url='$product_url' where products_id='$products_id' and stores_id='$stores_id'")->execute();
Yii::app()->db->createCommand(" update deal set title='$title' ,description='$description',expiry_ where id='$deal_id' ")->execute();
$command = "update real_property set p_delete=1 where p_id='$id' ";
}
}
//$this->redirect(array('index','page'=>$page));
echo "success";
exit(0);
//
//
}
You should download jphpexcelreader first.Plz comment to improve myself…