分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

net core 上传并使用EPPlus导入Excel文件

发布时间:2023-09-06 02:00责任编辑:傅花花关键词:暂无标签

1.  cshtml页面 form

<form id="form" method="post" action="/SaveValueBatch" ?????enctype="multipart/form-data"><input type="file" name="uploadExcel" style="width:200px;" /></form>

2. controller

 ???????[HttpPost] ???????public ActionResult SaveValueBatch(IFormCollection form) ???????{ ???????????try ???????????{ ???????????????var files =Request.Form.Files.Where(x => x.Name.Equals("uploadExcel")); ???????????????//非空限制 ???????????????if (files == null || files.Count() <= 0) { return Json(new { isSuccess = false, message = "请选择要上传的Excel文件" }, "text/html"); } ???????????????//格式限制 ???????????????var allowType = new string[] { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}; ???????????????if (files.Any(b => !allowType.Contains(b.ContentType))) ???????????????{ ???????????????????return Json(new { isSuccess = false, message = "只能上传Excel 2007 格式文件" }, "text/html"); ???????????????} ???????????????//大小限制 ???????????????if (files.Sum(b => b.Length) >= 1024 * 1024 * 4) ???????????????{ ???????????????????return Json(new { isSuccess = false, message = "上传文件的总大小只能在4M以下" }, "text/html"); ???????????????} ???????????????//写入服务器磁盘 ???????????????foreach (var file in files) ???????????????{ ???????????????????var fileName = file.FileName; ???????????????????var path = Path.Combine(_host.ContentRootPath+ "/Upload", fileName); ???????????????????using (var stream = System.IO.File.Create(path)) ???????????????????{ ???????????????????????file.CopyTo(stream); ???????????????????} ???????????????} ???????????????return Json(new { isSuccess = true, message = "保存成功" }, "text/html"); ???????????} ???????????catch (Exception e) ???????????{ ??????????????????????????????return Json(new { isSuccess = false, message = "保存失败:" + e.InnerException.Message }, "text/html"); ???????????} ???????}
 ???????private IHostingEnvironment _host; ???????public ExcelController(IHostingEnvironment host) ???????{ ???????????_host = host; ???????}

net core 上传并使用EPPlus导入Excel文件

原文地址:https://www.cnblogs.com/zitjubiz/p/net_core_epplus_excel_upload.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved