.net core ajax使用EPPlus上传excle导入总结
程序员文章站
2022-06-12 20:33:35
前端 后端 技术要点: 1.上传附件
前端
<form class="layui-form" id="div_imp" style="display:none;"> <div class="layui-row"> <div class="layui-col-xs11"> <div class="layui-form-item"> </div> </div> </div> <div class="layui-row"> <div class="layui-col-xs11"> <div class="layui-form-item"> <label for="goodsno" class="layui-form-label"> <span>附件</span> </label> <div class="layui-input-block"> <input type="file" name="goodsfile" id="goodsfile" class="layui-input" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"> </div> </div> </div> <div class="layui-col-xs11"> <div class="layui-form-item"> <label for="goodsno" class="layui-form-label"> <span></span> </label> <div class="layui-input-block"> <span class="we-red"><a href="~/import/商品导入模板.xlsx">点我下载最新模板</a></span> </div> </div> </div> </div> <div class="layui-row"> <div class="layui-form-item"> <label for="l_repass" class="layui-form-label"> </label> <button class="layui-btn" lay-filter="import" lay-submit="" onclick="import_submit()">导入</button> </div> </div> </form>
<javascript> function import_submit() { var file; $('#import').attr('disabled', true);//.find('i').removeclass('layui-hide'); var fordata = new formdata(); fordata.append("file", $('#goodsfile')[0].files[0]); //传给后台的file的key值是可以自己定义的 $.ajax({ url: "/goods/import", type: "post", data: fordata, cache: false, processdata: false, contenttype: false, success: function (res) { if (res.code == 0) { parent.layer.close(index); parent.layer.msg('导入成功', { time: 3000 }); parent.table.reload("test", { where: { "key": $("#goodsname").val() } }); } else { layer.msg("导入失败 " + res.msg, { time: 3000 }); $('#add').attr('disabled', false).find('i').removeclass('layui-hide'); } }, error: function (e) { $('#add').attr('disabled', false).find('i').removeclass('layui-hide'); layer.msg("导入失败 " + res.msg, { time: 3000 }); } }); } </script>
后端
[httppost]
public iactionresult import([fromform(name = "file")]iformfile excelfile)
{
handleresult<string> result = new handleresult<string>{ code=(int)statuscodeenum.error_system, success=false, msg=statuscodeenum.error_system.todescription() };
string swebrootfolder = _hostingenvironment.webrootpath+ @"\import_tmp\";
string sfilename = $"{datetime.now.tostring("yyyymmddhhmmssfff")}-{guid.newguid()}.xlsx";
fileinfo file = new fileinfo(path.combine(swebrootfolder, sfilename));
if (file.exists)
{
file.delete();
file = new fileinfo(path.combine(swebrootfolder, sfilename));
}
try
{
using (filestream fs = new filestream(file.tostring(), filemode.create))
{
excelfile.copyto(fs);
fs.flush();
}
using (excelpackage package = new excelpackage(file))
{
excelworksheet worksheet = package.workbook.worksheets[1];
int rowcount = worksheet.dimension.rows;
int colcount = worksheet.dimension.columns;
var goods = new list<pub_goods>();
if (file.exists)
{
file.delete();
}
for (int row =2; row <= rowcount; row++)
{
pub_goods pub_goods = new pub_goods();
pub_goods.goodsid = guid.newguid().tostring();
pub_goods.companyid = application.webcontext.companyid;
pub_goods.state = "1";
pub_goods.createtime = datetime.now.tostring("yyyy-mm-dd hh:mm:ss");
pub_goods.is_del = "0";
pub_goods.goodsno = worksheet.cells[row, 1].value == null ? "" : worksheet.cells[row,1].value.tostring();
pub_goods.goodsname = worksheet.cells[row,2].value == null ? "" : worksheet.cells[row, 2].value.tostring();
pub_goods.goodstype = worksheet.cells[row, 3].value == null ? "" : worksheet.cells[row, 3].value.tostring();
pub_goods.unit = worksheet.cells[row, 4].value == null ? "" : worksheet.cells[row, 4].value.tostring();
pub_goods.goodscode = worksheet.cells[row, 5].value == null ? "" : worksheet.cells[row, 5].value.tostring();
pub_goods.approval = worksheet.cells[row, 6].value == null ? "" : worksheet.cells[row, 6].value.tostring();
pub_goods.factory = worksheet.cells[row, 7].value == null ? "" : worksheet.cells[row, 7].value.tostring();
pub_goods.dosage = worksheet.cells[row, 8].value==null?"": worksheet.cells[row, 8].value.tostring();
goods.add(pub_goods);
}
result =_goodsservice.addlistasync(goods).result;
}
}
catch (exception ex)
{
logger.default.processerror((int)statuscodeenum.error_system, ex.message);
}
return ok(result);
}
技术要点:
1.上传附件<input type="file" name="goodsfile" id="goodsfile" class="layui-input" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
accept可设置默认允许的文件类型,也可选择其他文件类型,需要后台加判断限制文件类型.
2.使用iformfile一定要加上[fromform(name = "file")],否则iformfile为null,file为前端自定义的文件参数名
3.附件添加 var fordata = new formdata(); fordata.append("file", $('#goodsfile')[0].files[0]); processdata和contenttype一定要设为false