示例#1
0
        public RespCommonObject PrepareAndStartImport()
        {
            using (var reader = new StreamReader(new MemoryStream(Convert.FromBase64String(_data))))
            {
                String row     = reader.ReadLine();
                var    columns = row.Split(GlobalConsts.Splitter);
                if (columns.Length == 0 || columns.Length != _columns.Count)
                {
                    throw new ArgumetMissingException("Получены некорректные данные");
                }

                CheckAndFixColumnNames();
                var skuIndex = _columns.FirstOrDefault(x => x.Value == CSVColumns.SKU).Key;
                var dataList = new List <string[]>();
                var rowIndex = 1;
                while (reader.Peek() >= 0)
                {
                    row     = reader.ReadLine();
                    columns = row.Split(GlobalConsts.Splitter);
                    if (!_skuSet.Add(columns[skuIndex]))
                    {
                        throw new InvalidArgumentException("Обнаружено совпадение для поля SKU: " + columns[skuIndex] + " | Ряд № " + rowIndex);
                    }
                    dataList.Add(columns);
                    rowIndex++;
                }
                Model = new CSVImportModel(_columns, dataList);
                Model.StartImportData();
            }
            return(new RespCommonObject
            {
                ErrCode = Errors.ErrorCode.Success,
                DebugMessage = "Данные успешно импортированы",
            });
        }
示例#2
0
        public ActionResult Upload(HttpPostedFileBase upload)
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    var savepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebShared/Uploads");

                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }

                    string savefile = Path.Combine(savepath, upload.FileName);
                    upload.SaveAs(savefile);

                    var dt = new DataTable();

                    string fileLocation = Server.MapPath("~/WebShared/Uploads/") + upload.FileName;

                    string connString = string.Empty;


                    var csvOrXlsPath = string.Format("{0}/{1}", Server.MapPath("~/WebShared/Uploads/"), upload.FileName);

                    var extensionIndex = upload.FileName.LastIndexOf(".");

                    var extension = Path.GetExtension(upload.FileName);

                    if (extension == ".csv")
                    {
                        dt = UtilityCSV.ConvertCSVtoDataTable(csvOrXlsPath);
                    }
                    //Connection String to Excel Workbook
                    else if (extension.Trim() == "xls")
                    {
                        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + csvOrXlsPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                        dt         = UtilityCSV.ConvertXSLXtoDataTable(csvOrXlsPath, connString);
                    }
                    else if (extension.Trim() == "xlsx")
                    {
                        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + csvOrXlsPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                        dt         = UtilityCSV.ConvertXSLXtoDataTable(csvOrXlsPath, connString);
                    }

                    var list = new List <CSVImportModel>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        var newCSVImport = new CSVImportModel {
                            StudentId = dt.Rows[i][0].ToString()
                        };

                        list.Add(newCSVImport);
                    }


                    //var model = new CSVImportViewModel {
                    //    CSVImportList= list
                    //};

                    return(View());
                }
                else
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
            }
            return(View());
        }