导入Excel按钮:
<form method="post" enctype="multipart/form-data" id="formExcel" style="DISPLAY: inherit;padding: 5px 10px;" name="fileForm">
<input multiple type="file" name="file" accept=".xls,.xlsx" id="t_file"/>
<button type="button" class="layui-btn" id="btnEIIIS"><i class="layui-icon"></i>导入Excel</button>
<a href="~/Srcipt/广信新生信息.xlsx" download="download">广信新生信息模板</a>
</form>
模态框:
<div class="layui-card-body ">
<div class="layui-table-body layui-table-main" style="position:relative">
<table class="layui-hide" id="tbFAYAOCX" lay-filter="test"></table>
<div id="win" class="easyui-dialog" title="Excel文件数据" data-options="iconCls:'icon-save',closed:true" style="width:90%;display:none;width: 1157px;left: 66px;top: 106px;">
<div>
<input type="button" onclick="Sava_sql()" value="保存到数据库 ">
</div>
<table id="td_excel" class="easyui-datagrid" style="width:100%;height:407px;font-size: 14px;"data-options="rownumbers:true,singleSelect:true"><thead>
<tr>
<th data-options="field:'通知书批次',width:90">通知书批次</th>
。。。。。。。。。。。。。。。等等。。。。。。。。。。。。。。。。
</tr>
</thead>
</table>
</div>
</div>
</div>
导入按钮事件:
//导入
layui.use('layer', function () {
$("#btnEIIIS").on("click",function () {
if ($('#t_file').get(0).files != null && $('#t_file').get(0).files.length > 0) {
var form = $("form[name=fileForm]");
var options = {
url: '/AssetMatchingInformation/AssetMatchingInformation/DaoRuExcel',
type: 'post',
success: function (data) {
//数据库用户
if (data['content'] != null && data['content'] != '') {
data = eval(data['content']);
} else {
data = [];
}
$('#win').window('open'); // 打开显示EXCEL数据的弹窗
$("#td_excel").datagrid('loadData', data);
}
};
form.ajaxSubmit(options);
} else {
alert('请选择文件再导入!!!');
}
});
});
控制器:
public ActionResult InertExamineeMessageLeads(){
string returnstr = "";
try
{
int saveCount = 0;//保存成功的条数
int oldCount = 0;//已经存在的数据条数
DataTable dt = (DataTable)Session["dt"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string mingzi = dt.Rows[i]["身份证号码"].ToString();
int intOld = (from SYS_学生表 in myModels.SYS_学生表
join SYS_学生信息表 in myModels.SYS_学生信息表 on SYS_学生表.学生ID equals SYS_学生信息表.学生ID
join SYS_通知表 in myModels.SYS_通知表 on SYS_学生表.学生ID equals SYS_通知表.学生ID
join PW_交费表 in myModels.PW_交费表 on SYS_学生表.学生ID equals PW_交费表.学生ID
where SYS_学生表.身份证号 == mingzi
select SYS_学生表).Count();
//if没有重复
if (intOld == 0)
{
//保存学生、
SYS_学生表 student = new SYS_学生表();
SYS_学生信息表 student1 = new SYS_学生信息表();
SYS_通知表 student2 = new SYS_通知表();
PW_交费表 student3 = new PW_交费表();
student.姓名 = dt.Rows[i]["姓名"].ToString().Trim();
myModels.SYS_学生信息表.Add(student1);
myModels.SaveChanges();
//新增的字段,以及表
saveCount++;
}
else
{
oldCount++;
}
}
returnstr = "导入" + dt.Rows.Count + "条数据,存在的数据有" + oldCount + "条,成功保存了"
+ saveCount + "到数据库";
}
else
{
returnstr = "没有要保存的";
}
}
catch (Exception e)
{
returnstr = "数据不能为空值!!!";
Console.WriteLine(e);
}
return Json(returnstr, JsonRequestBehavior.AllowGet);
}//保存
保存按钮:
//保存导入数据
function Sava_sql() {
var layerIndex = layer.load();
$.post("/AssetMatchingInformation/AssetMatchingInformation/InertExamineeMessageLeads", function (msg) {
layer.close(layerIndex);
layer.msg(msg, { offset: 250 });
$("#td_excel").modal("hide");
setTimeout(function () {
location.reload();
});
})
}
以上功能是:Excel表格导入数据,然后新增多表保存到数据库。