Dapper导入导出或上传下载Excel


一、后台Api操作

    下载包: MiniExcel

    下载包:Dapper.SimpleCRUD

    下载包: Z.Dapper.Plus

     引用

    using System.IO;
    using System.Linq;
    using MiniExcelLibs;
    using MiniExcelLibs;

    1、Dal数据访问层

        /// 
        /// 导入 上传 Excel
        /// 
        /// 
        /// 
        public int Uploading(List user)
        {
            using (var conn = new SqlConnection(_configuration.GetConnectionString("MSSQL")))
            {
                var list = conn.BulkInsert(user);
                if(list!=null)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

    2、Bll业务逻辑层

        /// 
        /// 导入 上传 Excel
        /// 
        /// 
        /// 
        public int Uploading(List user)
        {
            try
            {
                return _userAllocationDal.Uploading(user);
            }
            catch (Exception)
            {

                throw;
            }
        }

    3、UI表现层 (控制器)

        /// 
        ///  导入 上传 Excel
        /// 
        /// 
        /// 
        [HttpPost]
        public IActionResult Uploading()
        {
            try
            {
                var excel = this.HttpContext.Request.Form.Files[0];
                var stream = new MemoryStream();
                excel.CopyTo(stream);
                var list = stream.Query().ToList();
                return Ok(_userAllocationBll.Uploading(list));
            }
            catch (Exception)
            {

                throw;
            }
        }
/// 
        /// 下载 导出 Excel 只有UI层
        /// 
        /// 
       [HttpGet]
        public IActionResult DownloadExcel()
        {
            
            var totalCount = 0;
            //根据你所选中的方法 写要求  分页如下   无要求为空
            var values = _userAllocationBll.AllShowUser( out totalCount, 1,10);
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values.ToList());
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "demo.xlsx"
            };
        }

二、前台Vue





    效果图

      ......待续