这篇文章主要讲解了“yii2.0框架如何实现上传excel文件后导入到数据库”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“yii2.0框架如何实现上传excel文件后导入到数据库”吧!
Model模型
<?php
/**
* 描述...
* @author zcy
* @date 2019/8/13
*/
namespace app\models;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
class uploadForm extends ActiveRecord
{
public $file;
public function rules()
{
return [
[['file'],'file', 'skipOnEmpty' => false,'extensions' => 'xls,xlsx'],
];
}
public function attributeLabels()
{
return [
'file'=> '上传文件'
];
}
public function upload()
{
$file = UploadedFile::getInstance($this, 'file');
if ($this->rules()) {
$tmp_file = $file->baseName . '.' . $file->extension;
$path = 'upload/' . 'Files/';
if (is_dir($path)) {
$file->saveAs($path . $tmp_file);
} else {
mkdir($path, 0777, true);
}
$file->saveAs($path . $tmp_file);
return true;
} else {
return '验证失败';
}
}
}
Views视图
<?php
use yii\widgets\ActiveForm;
$model = new app\models\uploadForm();
$form = ActiveForm::begin([
'id' => 'upload',
'options' => ['enctype' => 'multipart/form-data'],
])
?>
<?= $form->field($model,'file')->fileInput(['multiple'=>'multiple']) ?>
<button>上传</button>
<?php ActiveForm::end() ?>
Controller控制器
<?php
/**
* 描述...
* @author zcy
* @date 2019/8/16
*/
namespace app\controllers;
use app\models\uploadForm;
use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
class UploadController extends Controller
{
/**
* 导入
* @author zcy
* @date 2019/8/16
*/
public function actionImport()
{
$model = new uploadForm();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model,'file');
// if ($model->upload()) {
// print <<<EOT
// <script>alert('上传成功')</script>
//EOT;
// } else {
// print <<<EOT
// <script>alert('上传失败')</script>
//EOT;
// }
if (!$model->upload()) {
print <<<EOT
<script>alert('上传失败')</script>
EOT;
}
}
$ok = 0;
if ($model->load(Yii::$app->request->post())) {
$file = UploadedFile::getInstance($model,'file');
if ($file) {
$filename = 'upload/Files/' . $file->name;
$file->saveAs($filename);
if (in_array($file->extension,array('xls','xlsx'))) {
$fileType = \PHPExcel_IOFactory::identify($filename);//文件名自动判断类型
$excelReader = \PHPExcel_IOFactory::createReader($fileType);
$phpexcel = $excelReader->load($filename)->getSheet(0);//载入文件并获取第一个sheet
$total_line = $phpexcel->getHighestRow();//总行数
$total_column = $phpexcel->getHighestColumn();//总列数
if (1 < $total_line) {
for ($row = 2;$row <= $total_line;$row++) {
$data = [];
for ($column = 'A';$column <= $total_column;$column++) {
$data[] = trim($phpexcel->getCell($column.$row));
}
$info = Yii::$app->db->createCommand()
->insert('{{%shop_info}}',['shop_name' => $data[0],'shop_type' => $data[1]])
->execute();
if ($info) {
$ok = 1;
}
}
}
if ($ok == 1) {
echo "<script>alert('导入成功');window.history.back();</script>";
} else {
echo "<script>alert('操作失败');window.history.back();</script>";
}
}
}
} else {
return $this->render('import',['model' => $model]);
}
}
}
感谢各位的阅读,以上就是“yii2.0框架如何实现上传excel文件后导入到数据库”的内容了,经过本文的学习后,相信大家对yii2.0框架如何实现上传excel文件后导入到数据库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。