Пример #1
0
        //文件上传
        public ActionResult UploadExcel(HttpPostedFileBase Fdata, string excelKey, string vaildURL, string saveURL)
        {
            LogWriter.Info(string.Format("UploadExcel - Excel导入的Key:{0} - 开始", excelKey));

            if (Fdata == null || Fdata.InputStream == null)
            {
                return(RedirectToAction("ImportExcel", new { excelkey = excelKey, vaildURL = vaildURL, saveURL = saveURL, ErrorMsg = "数据文件没有上传,请上传要导入数据文件!" }));
            }

            var fileSize = Fdata.InputStream.Length;

            byte[] fileBuffer = new byte[fileSize];
            Fdata.InputStream.Read(fileBuffer, 0, (int)fileSize);
            Fdata.InputStream.Close();

            ExcelData data = null;

            try
            {
                IImporter importer = new AsposeExcelImporter();
                data = importer.Import(fileBuffer, excelKey);
            }
            catch (Exception ex)
            {
                LogWriter.Error(ex);
                return(RedirectToAction("ImportExcel", new { excelkey = excelKey, vaildURL = vaildURL, saveURL = saveURL, ErrorMsg = ex.Message }));
            }
            var serverUrl = string.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority);

            // 验证数据
            LogWriter.Info(string.Format("UploadExcel - 进入校验数据逻辑,校验地址:{0} - 开始", vaildURL));
            var errors = Post <List <CellErrorInfo> >(serverUrl, vaildURL, new { data = JsonConvert.SerializeObject(data) });

            LogWriter.Info(string.Format("UploadExcel - 进入校验数据逻辑,校验地址:{0} - 结束", vaildURL));
            if (errors.Count > 0)
            {
                var errorFilePath = string.Format(@"~/ErrorExcels/{0}_有错误.xls", excelKey);
                WriteErrorInfoToExcel(errors, fileBuffer, errorFilePath);
                return(RedirectToAction("ImportExcel", new { excelkey = excelKey, vaildURL = vaildURL, saveURL = saveURL, ErrorCount = errors.Count, ErrorFilePath = Url.Content(errorFilePath) }));
            }

            // 保存数据
            var dataTable = data.GetDataTable();

            LogWriter.Info(string.Format("UploadExcel - 进入数据保存逻辑,保存地址:{0} - 开始", saveURL));
            var strResult = Post(serverUrl, saveURL, new { data = JsonConvert.SerializeObject(dataTable, new DataTableConverter()) });

            LogWriter.Info(string.Format("UploadExcel - 进入数据保存逻辑,保存地址:{0} - 结束", saveURL));
            if (strResult != "Success")
            {
                LogWriter.Error(strResult);
                return(RedirectToAction("ImportExcel", new { excelkey = excelKey, vaildURL = vaildURL, saveURL = saveURL, ErrorMsg = "数据无法保存,请联系管理员查看错误日志!" }));
            }
            // 清空数据,释放内存
            data = null;

            LogWriter.Info(string.Format("UploadExcel - Excel导入的Key:{0} - 结束", excelKey));
            return(RedirectToAction("ImportExcel", new { excelkey = excelKey, vaildURL = vaildURL, saveURL = saveURL, result = dataTable.Rows.Count.ToString() }));
            //return Content(strResult);
        }