1.composer 下载 :composer require phpoffice/phpexcel
<< ++ 下载完后 把文件 :phpoffice/phpexcel/Classes 改名 PHPExecl 移动到 vendor 目录下 ++ >>
2.前端html页面:
<<< ++
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>学校管理</title>
<link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<div class="layui-fluid" >
<form method="post" action="" class="form-signin" enctype="multipart/form-data" >
<input name="excel" type="file" class="form-control">
<button class="btn btn-lg btn-primary btn-block">导入</button>
</form>
</div>
<script src="/static/layui/layui.js" charset="utf-8"></script>
<script>
layui.use(['laypage', 'table', 'element', 'layer', 'jquery','form'], function () {
var table = layui.table;
layer = layui.layer;
laypage = layui.laypage;
element = layui.element;
form = layui.form;
$=layui.$;
});
</script>
</body>
</html>
++ >>
3.控制器方法:
<< ++
// 导入学生
public function daoru(Request $request){
//上传excel文件
$file = request()->file('excel');
if(!$file){
return view("daoru");
}
$ft=$file->getInfo("type");
//halt($ft);
if($ft !="application/vnd.ms-excel" && $ft !="application/octet-stream" &&$ft !="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
return $this->error("请选择正确的excel表格");
}
//移到/public/uploads/excel/下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel');
//halt($info);
//上传文件成功
if ($info) {
//引入PHPExcel类
vendor('PHPExcel.PHPExcel');
new PHPExcel();
//获取上传后的文件名
$fileName = $info->getSaveName();
//获取上传文件的后缀
$fileType = substr(strchr($fileName, "."), 1);
//文件路径
$filePath = 'public/uploads/excel/' . $fileName;
//实例化PHPExcel类
if ($fileType == 'xls') {
$PHPReader = new \PHPExcel_Reader_Excel5(); //设置以Excel5格式(Excel97-2003工作簿)
}
if ($fileType == 'xlsx') {
$PHPReader = new \PHPExcel_Reader_Excel2007();
}
//读取excel文件
$objPHPExcel = $PHPReader->load(ROOT_PATH . $filePath);
//读取excel文件中的第一个工作表
$sheet = $objPHPExcel->getSheet(0);
$allRow = $sheet->getHighestRow(); //取得总行数
//$allColumn = $sheet->getHighestColumn(); //取得总列数
//从第二行开始插入,第一行是列名
for ($j = 2; $j <= $allRow; $j++) {
/* 编辑时启用
$da['id'] = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();*/
$data['img'] = $objPHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();
$data['tel'] = $objPHPExcel->getActiveSheet()->getCell("C" . $j)->getValue();
$data['name'] = $objPHPExcel->getActiveSheet()->getCell("D" . $j)->getValue();
$data['sfz'] = $objPHPExcel->getActiveSheet()->getCell("E" . $j)->getValue();
$data['school'] = $objPHPExcel->getActiveSheet()->getCell("F" . $j)->getValue();
$data['pro_id'] = $objPHPExcel->getActiveSheet()->getCell("G" . $j)->getValue();
// 判断接收的时间是否是float类型,如果是,转换时间戳的时候需要减掉8个小时的时差
if( is_float($objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue())){
$data["create_time"] =\PHPExcel_Shared_Date::ExcelToPHP($objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue())-8*60*60;
}else{
// 如果是字符串类型则可直接转换时间戳
$data["create_time"]=strtotime($objPHPExcel->getActiveSheet()->getCell("H" . $j)->getValue());
}
$data['status'] = $objPHPExcel->getActiveSheet()->getCell("I" . $j)->getValue();
$data['sex'] = $objPHPExcel->getActiveSheet()->getCell("J" . $j)->getValue();
$data['money'] = $objPHPExcel->getActiveSheet()->getCell("K" . $j)->getValue();
$data['vip'] = $objPHPExcel->getActiveSheet()->getCell("L" . $j)->getValue();
$data['vip_lv'] = $objPHPExcel->getActiveSheet()->getCell("M" . $j)->getValue();
/* 编辑时启用
$result=Db::name('users')->where("id",$da['id'])->update($data);*/
//添加时用
$result = Db::name("cms_member")->insertGetId($data);//保存数据,并返回主键id
if ($result) {
// 添加时用
echo "第" . $j . "行导入成功,comment表第:" . $result . "条!<br/>";
/*编辑时启用
echo "第".$j."行导入成功,users表第:".$da['id']."条!<br/>";*/
} else {
echo "第" . $j . "行导入失败!<br/>";
}
}
} else {
echo "导入文件失败!";
}
return $this->success("导入成功",url("index"));
}
++ >>
4.导出
<< ++
// html代码
<div class="fbutton">
<a href="{:U('Admin/comment/out')}">
<div class="add" title="导出">
<span>导出模板</span>
</div>
</a>
</div>
// php 方法
/**
* 导出模板
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function out()
{
vendor('PHPExcel.PHPExcel.Reader.Excel5');
//实例化PHPexcel类,等同于新建一个excel
$objPHPExcel = new \PHPExcel();
for($i=1;$i<=3;$i++){
if($i>1){
$objPHPExcel->createSheet();//创建内置表
}
}
$objPHPExcel->setActiveSheetIndex(0);//把新创建的sheet 设定为当前活动的sheet
//$objPHPExcel->getActiveSheet()获取当前活动的sheet
//给当前活动sheet 设置每列的表头
$objPHPExcel->getActiveSheet()->
setCellValue("A1", '评论id(添加评论不填)')->
setCellValue("B1", '商品id(不能为空,且需与商品库一致)')->
setCellValue("C1", 'email邮箱(请留空)')->
setCellValue("D1", '用户名(不能为空,且与用户名一致)')->
setCellValue("E1", '评论内容(不能为空)')->
setCellValue("F1", '添加时间(格式为:2018/9/6 11:05:15)')->
setCellValue("G1", 'ip地址(请留空)')->
setCellValue("H1", '是否显示(只能为1或0;1显示,0不显示)')->
setCellValue("I1", '父id (请留空)')->
setCellValue("J1", '评论用户ID(不能为空,且与用户名ID一致)')->
setCellValue("K1", '晒单图片(请留空)')->
setCellValue("L1", '订单id(请留空)')->
setCellValue("M1", '物流评价等级(不能为空,且只能为1-5)')->
setCellValue("N1", '商品评价等级(不能为空,且只能为1-5)')->
setCellValue("O1", '商家服务态度评价等级(不能为空,且只能为1-5)')->
setCellValue("P1", '被赞数(请留空)')->
setCellValue("Q1", '点赞用户id(请留空)')->
setCellValue("R1", '是否匿名评价:只能是0或1;0不是,1是')->
setCellValue("S1", '订单商品表ID (请留空)')->
setCellValue("T1", '排序(填写100以内的数字)');
// 获取评论数据
$data=Db::name("comment")->limit(5)->order("comment_id desc")->select();
// halt($data);
// 循环填写excel表
$j=2;
foreach ($data as $k =>$v){
$data["add_time"]=date('Y/n/j G:i:s',$v["add_time"]);
$objPHPExcel->getActiveSheet()->
setCellValue("A".$j,$v["comment_id"])->
setCellValue("B".$j,$v["goods_id"])->
setCellValue("C".$j,$v["email"])->
setCellValue("D".$j,$v["username"])->
setCellValue("E".$j,$v["content"])->
setCellValue("F".$j,$data["add_time"])->
setCellValue("G".$j,$v["ip_address"])->
setCellValue("H".$j,$v["is_show"])->
setCellValue("I".$j,$v["parent_id"])->
setCellValue("J".$j,$v["user_id"])->
setCellValue("K".$j,$v["img"])->
setCellValue("L".$j,$v["order_id"])->
setCellValue("M".$j,$v["deliver_rank"])->
setCellValue("N".$j,$v["goods_rank"])->
setCellValue("O".$j,$v["service_rank"])->
setCellValue("P".$j,$v["zan_num"])->
setCellValue("Q".$j,$v["zan_userid"])->
setCellValue("R".$j,$v["is_anonymous"])->
setCellValue("S".$j,$v["rec_id"])->
setCellValue("T".$j,$v["sort"]);
$j++;
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition:attachment;filename="template.xls"');
// D:\shop\vendor\PHPExcel\PHPExcel\Calculation\Functions.php on line 570 的break 需删除
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
$objPHPExcel->disconnectWorksheets();
}
网友评论