1、layui导入,导出,下载样式
2、js部分
//导入 用layui upload插件layui.use([ "element", "laypage", "layer", "upload"], function() {
var element = layui.element;
var laypage = layui.laypage;
var layer = layui.layer;
var upload = layui.upload;//主要是这个
layui.upload.render({
elem: "#importData",//导入id
url: "{:url('uploadFile'}",
size: '3072',
accept: "file",
exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
done: function (result) {
layer.msg(result.msg);
widow.location.reload();
}
}
});
//refreshTable()
});
//导出
function exportData() {
$.ajax({
type: "post", url: "/xx/exportData", data: {}, success: function (result) {
if (result.status == 0) {
window.open(result.data)
}
if (result.message != null) {
layer.msg(result.message)
}
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
}
})
};
//模板下载
function downloadTemplate() {
$.ajax({
type: "post", url: "/xx/downloadTemplate", data: {}, success: function (result) {
if (result.status == 0) {
window.open(result.data)
}
if (result.message != null) {
layer.msg(result.message)
}
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}')
}
})
}
3、控制器部分
首先下载phpexcel
PHPExcel 扩展包下载地址: https://github.com/PHPOffice/PHPExcel
下载压缩包只需要解压后classes文件夹里的所有内容。
将其放在一个你自定义名字的文件夹里面,本人用的是PHPExcel,然后将文件夹放在extend里面,其余看代码
//导入
public function uploadFile()
{
// 引入核心文件
require'../extend/PHPExcel/PHPExcel.php';
header("content-type:text/html;charset=utf-8");
//上传excel文件
$file = request()->file('file');
$files = $_FILES['file'];
//将文件保存到public/uploads目录下面
$info = $file->validate(['size'=>1048576,'ext'=>'xls,xlsx'])->move( './uploads');
if($info){
$exclePath = $info->getSaveName(); //获取文件名
$file_name = './uploads/'. $exclePath; //上传文件的地址
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
foreach ($excel_array as $k => $v){
$arr['school'][$k] = $v[0];
$arr['grade'][$k]['school'] = $v[0];
$arr['grade'][$k]['grade'] = $v[1];
$arr['class'][$k]['school'] = $v[0];
$arr['class'][$k]['grade'] = $v[1];
$arr['class'][$k]['class'] = $v[2];
}
// var_dump($arr);
$arr['school']=array_unique($arr['school']);
// $arr['grade']=$this->assoc_unique($arr['grade'],"grade");
// $arr['class']=$this->assoc_unique($arr['class'],"class");
$res2=1;
// var_dump($arr);
foreach ($arr['school'] as $k=>$v){
$result=Db::name("school")->where("name",$v)->find();
if(empty($result)){
$arrary['pid']=0;
$arrary['name']=$v;
$arrary['create_time']=time();
$res2=Db::name("school")->insert($arrary);
}
}
foreach ($arr['grade'] as $k=>$v){
$pid=Db::name("school")->where("name",$v['school'])->value('id');
if(!empty($pid)){
$result=Db::name("school")->where("name",$v['grade'])->where("pid",$pid)->find();
if(empty($result)){
$arrary['pid']=$pid;
$arrary['name']=$v['grade'];
$arrary['create_time']=time();
$res2=Db::name("school")->insert($arrary);
}
}
}
foreach ($arr['class'] as $k=>$v){
$pid1=Db::name("school")->where("name",$v['school'])->value('id');
if(!empty($pid1)){
$pid=Db::name("school")->where("name",$v['grade'])->where("pid",$pid1)->value('id');
if(!empty($pid)){
$result=Db::name("school")->where("name",$v['class'])->where("pid",$pid)->find();
if(empty($result)){
$arrary['pid']=$pid;
$arrary['name']=$v['class'];
$arrary['create_time']=time();
$res2=Db::name("school")->insert($arrary);
// var_dump(33);
}
}
}
}
if($res2){
return $this->success("导入成功",'index/index');
}else{
return $this->error("导入失败",'index/index');
}
}
}