我在MySQL数据库中有客户表,我在excel中有客户列表,我想将excel数据导入到MySQL客户表中,我希望有人一步一步地告诉我如何做到这一点,并告诉模型视图和控制器。
我已经编写了客户表的MySQL查询,以表明我将是在excel csv中相同的颜色。
(`CustomerID`, `FirstName`, `LastName`, `JobTitle`, `BusinessPhone`, `MobilePhone`, `FaxNumber`, `Address`, `Area`, `State`, `ZipCode`, `Country`, `Email`, `Webpage`, `Notes`, `CustomerInvoice`, `Status`)您想告诉我如何将csv数据导入MySQL表,还是有插件将MySQL数据导入MySQL数据库?
发布于 2016-03-01 13:11:29
如果我正确理解,您可以在控制器操作中做一些事情。
public function actionYourActionName(){
if (isset($_FILES['csv_file']) && !empty($_FILES['csv_file'])) {
$csv = array();
$file = fopen($_FILES['csv_file']['tmp_name'], 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
$csv[] = $line;
}
fclose($file);
for ($i = 1; $i < count($csv); $i++) {
$model = new YourmodelName();
foreach ($csv[0] as $key => $value) {
$model->$value = $csv[$i][$key];
}
if($model->save()){
//do here what you want to do after saving model
}else{return $model->getErrors();}
}
}
}else{
$this->render('your view name');
}在你看来,文件就像。
echo CHtml::form('', 'post', array('id' => "verticalForm", 'class' => 'well form-vertical', 'enctype' => 'multipart/form-data'));
echo CHtml::fileField('csv_file[]', '', array('id' => 'csv_file', 'multiple' => 'multiple'));
echo '<p class="help-block">Please upload .csv files only.</p>';
echo CHtml::submitButton('Submit', array('class' => 'btn btn-primary'));
echo CHtml::endForm();我想您已经创建了一个model.for您的mysql表,希望这将帮助您。
发布于 2016-02-24 07:00:06
这是最好的插件,有适当的文档和示例。
http://phpexcel.codeplex.com/
使用这个插件,您可以在mysql中导入excel数据。
发布于 2016-02-24 07:02:29
您可以使用“加载数据”命令。以下是yiiframework文档中的示例。
http://www.yiiframework.com/wiki/336/importing-csv-file-to-mysql-table-using-load-data-command/
https://stackoverflow.com/questions/35595257
复制相似问题