说明:
前台使用layui插件 非常简单 不会的去看文档 没有任何难度 地址 https://www.layui.com/
使用的PHP框架是CI
HTML页面
<form class="layui-form" action="/admin/Material_purchase/import_data" method="post" target="frame1" enctype="multipart/form-data">
<div class="layui-form-item">
<div class="layui-inline">
<div class="layui-input-inline" style="width:300px">
<input type="text" name="purchaser" id="purchaser" class="layui-input" placeholder="账户名称或手机号" autocomplete="off"/>
</div>
<div class="layui-input-inline" style="width:300px">
<input type="text" name="material" id="material" class="layui-input" placeholder="物料编码" autocomplete="off"/>
</div>
<div class="layui-input-inline" style="width:100px">
<a class="layui-btn" id="search">立即查询</a>
</div>
<div class="layui-input-inline" style="width:100px">
<a class="layui-btn" id="add">添加</a>
</div>
//批量导入按钮---------------------------------------------------------
<div class="layui-input-inline" style="width:100px">
<a type="button" class="layui-btn" id="import" style="width:100px;margin-left: -30px">批量导入</a>
</div>
</div>
</div>
</form>
<table id="table" lay-filter="table"></table>
<iframe name="frame1" frameborder="0" height="40"></iframe>
<script>
$('body').css('background-color','#fff');
layui.use(['form','table', 'layer', 'upload'],function(){
var form = layui.form,
table = layui.table,
layer = layui.layer,
upload = layui.upload
var tableIns = table.render({
elem: '#table',
height: 'full-140',
url: "/admin/material_purchase/get_data.html",
limit: {{ page }},
limits: [{{ page*1 }},{{ page*2 }},{{ page*3 }},{{ page*4 }},{{ page*5 }}],
page: true,
even: true,
id: "table",
cols: [[
{field: "id", title: "ID", width: "88"},
{field: "name", title: "账户名称", width: "200"},
{field: "phone", title: "手机号", width: "200"},
{field: "material_code", title: "物料编码", width: "200"},
{field: "gmt_create", title: "创建时间", width: "200"},
{field: "operation", title: "操作", width: "200"}
]]
})
var uploadInst = upload.render({
elem: '#import' //绑定元素
,url: '/admin/material_purchase/import_data' //上传接口对应的方法
,accept: 'file'
,done: function(res){
layer.msg(res.msg);
tableIns.reload();
}
,error: function(){
//请求异常回调
}
});
})
</script>
PHP页面
public function import_data()
{
try{
if ($_FILES["file"]["error"] > 0) {
throw new Exception('');
}
//只允许上传excel表格
$extension = substr(strrchr($_FILES['file']['name'], '.'), 1);
if ($extension != 'xlsx' && $extension != 'xls') {
throw new Exception('上传文件格式错误');
}
//重命名
$fileName = 'MATERIAL_PURCHASE'.time().rand(1000000, 9999999);
//文件存储路径
$pathName = FCPATH . 'upload/files/' .$fileName .$extension;
$res = move_uploaded_file($_FILES["file"]['tmp_name'],$pathName);
//加载PHPExcel
$this->load->library('PHPExcel/IOFactory');
if ($res) {
$type = $extension == 'xlsx' ? 'Excel2007' : 'Excel5';
$objReader = $this->iofactory->createReader($type);
$objPHPExcel = $objReader->load($pathName);
$sheet = $objPHPExcel->getSheet(0);//获取第一个excel里的sheet
$highestRow = $sheet->getHighestRow();
if ($highestRow > 10001) {
throw new Exception('至多一次导入10000条数据');
}
$arr_A = [];
$arr_B = [];
for ($i=2; $i<= $highestRow; $i++) {
$k_A = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();//获取每一个的值
$k_B = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
array_push($arr_A, $k_A);
array_push($arr_B, $k_B);
}
$arr_A_id = [];
foreach ($arr_A as $key => $value) {
$uid = $this->accountModel->getFieldValue('id', ['account_name'=>$value,'is_locked'=>0]);
if (!$uid) {
throw new Exception('用户'.$value.'已锁定');
}
array_push($arr_A_id, $uid);
}
$data = [];
for ($i=2; $i<=$highestRow; $i++) {
$data[$i-2][0] = $arr_A_id[$i-2];
$data[$i-2][1] = $arr_B[$i-2];
}
$gmt_create = Carbon::now()->toDateTimeString();
$gmt_modified = Carbon::now()->toDateTimeString();
//组装插入语句
$sql = 'INSERT INTO swap_material_purchase(purchaser, material_code, gmt_create, gmt_modified) VALUES ';
foreach ($data as $key => $value) {
if ($value == end($data)) {
$sql .= "("."'$value[0]'".","."'$value[1]'".","."'$gmt_create'".",
"."'$gmt_modified'".");";
} else {
$sql .= "("."'$value[0]'".","."'$value[1]'".","."'$gmt_create'".",
"."'$gmt_modified'"."),";
}
}
//插入数据
$result = $this->materialPurchaseModel->query($sql);
if ($result) {
$msg = '导入成功';
} else {
$msg = '导入失败';
}
$data = [
'msg' => $msg
];
echo json_encode($data);
} else {
throw new Exception('导入失败');
}
}catch(Exception $e){
$msg = $e -> getMessage();
$data = [
'msg' => $msg
];
echo json_encode($data);
}
}
excel 内容格式如下:
效果图: