Layui导入CSV文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Data</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
<link rel="stylesheet" href="../css/public.css" media="all">
<style>
.layui-table-cell {
font-size: 14px;
padding: 0 5px;
height: auto;
overflow: visible;
text-overflow: inherit;
white-space: normal;
word-break: break-all;
}
</style>
</head>
<body>
<div class="layuimini-container">
<div>
<div class="layuimini-main">
<button type="button" class="layui-btn" id="ImportTable">
<i class="layui-icon"></i>导入数据
</button>
<button type="button" class="layui-btn layui-btn-normal" id="Import">
<i class="layui-icon"></i>提交数据
</button>
</div>
<table class="layui-hide" id="ImportTableId" lay-filter="ImportTableFilter"></table>
</div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
layui.config({
base: '../layui_exts/'
}).extend({
excel: 'excel'
});
layui.use(['form', 'table', 'upload', 'excel'], function () {
var $ = layui.jquery,
form = layui.form,
table = layui.table;
var layer = layui.layer;
var upload = layui.upload;
var da = [];
$(document).on('click', "#Import", function () {
if (da.length == 0) {
layer.alert("请先导入表格数据,检查数据格式是否正确,然后再提交数据!");
return;
}
var daa = da;
$.ajax({
type: "post",
url: "../../Handler/DataProcess.ashx?action=Import",
data: JSON.stringify({ "data": daa }),
dataType: "json",
contentType: 'application/json',
success: function (data) {
if (data.data > 1) {
alert("数据导入成功!!!");
var iframeIndex = parent.layer.getFrameIndex(window.name);
parent.layer.close(iframeIndex);
parent.layui.table.reload('currentTableId');
} else {
layer.alert("数据导入失败!!!请检查数据格式是否正确!");
}
}
});
});
table.render({
elem: '#ImportTableId',
data: da,
height: 'full-5',
cols: [[
{ field: 'ServosNumber', width: 150, title: '伺服数量', sort: true, align: "center", edit: 'text' }
]],
limit: 1000
});
var uploadInst = upload.render({
elem: '#ImportTable',
url: '',
accept: 'file',
exts: 'csv',
acceptMime: '.csv',
auto: false,
before: function (obj) {
layer.load();
},
choose: function (obj) {
var files = obj.pushFile();
var fileArr = Object.values(files);
for (var index in files) {
if (files.hasOwnProperty(index)) {
delete files[index];
}
}
uploadExcel(fileArr);
},
done: function (res) {
},
error: function () {
setTimeout(function () {
layer.msg("上传失败!", {
icon: 1
});
}, 1000);
}
});
function uploadExcel(files) {
try {
if (files.length < 1) {
alert("传入文件为空!!");
}
var supportReadMime = [
'application/vnd.ms-excel',
'application/msexcel',
'application/x-msexcel',
'application/x-ms-excel',
'application/x-excel',
'application/x-dos_ms_excel',
'application/xls',
'application/x-xls',
'application/vnd-xls',
'text/csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
''
];
layui.each(files, function (index, item) {
if (supportReadMime.indexOf(item.type) === -1) {
alert(item.name + '(' + item.type + ')为不支持的文件类型');
}
});
if (files[0].type === "text/csv") {
let reader = new FileReader();
reader.readAsText(files[0], 'GB2312');
reader.onload = function (ev) {
var result = ev.target.result;
let arr = result.split('\n');
for (var i = 1; i < arr.length; i++) {
let d = arr[i].split(',');
var a = { ServosNumber: d[0], PLCType: d[1], ThirdParty: d[2], Limit: d[3] }
da[i - 1] = a;
}
layui.table.reload('ImportTableId');
layer.alert("请检查表格数据是否正确再提交数据!");
}
}
} catch (e) {
layer.alert(e.message);
}
}
});
</script>
</body>
</html>