Yii

Import data from CSV sheet to Database in Yii

In this post we will see how we can Import data from CSV sheet to Database in Yii.

For this You just have to download a JPhpExcelReader Extension.


$itu = CUploadedFile::getInstance($model,'csv_file');
$data = new JPhpExcelReader($itu->getTempName());

$rows = array();
for($j=1; $j<=$data->sheets[0]['numRows']; $j++) {
if($j == 1){
$header = $data->sheets[0]['cells'][$j];

}else{
$rowData = $data->sheets[0]['cells'][$j];
$rows[] = array_combine($header,$rowData);
}
}
//Here in $rows you will get all csv sheet data in array format
foreach($rows as $row)
{

//define model
//fieldnames to save in model
//save data in database

}

If you find it helpful, Please like this post and share it with your friends.

Leave a Reply