Пример #1
0
        public bool FileToEntity(HttpPostedFile file, string modelType)
        {
            bool isSuccess = true;

            if (string.Empty.Equals(file.FileName) || ".xlsx" != Path.GetExtension(file.FileName))
            {
                isSuccess = false;
                throw new ArgumentException("当前文件格式不正确,请确保正确的Excel文件格式!");
            }
            var severPath = this.Server.MapPath("/files/");         //获取当前虚拟文件路径
            var savePath  = Path.Combine(severPath, file.FileName); //拼接保存文件路径

            try
            {
                file.SaveAs(savePath);
                if (string.Equals(modelType, "app"))
                {
                    appEntityList = ExcelExtension.LoadFromExcel <RepairApplication>(savePath).ToList();
                }
                if (string.Equals(modelType, "dev"))
                {
                    devEntityList = ExcelExtension.LoadFromExcel <Device>(savePath).ToList();
                }
            }
            catch { isSuccess = false; }
            finally
            {
                System.IO.File.Delete(savePath);//每次上传完毕删除文件
            }
            return(isSuccess);
        }