public ActionResult <ImportSiteResponseModel> ImportSite([FromBody] ImportSiteInputModel data)
        {
            ImportSiteResponseModel res = new ImportSiteResponseModel();

            LogAppBL blLog = new LogAppBL(DbContext);

            try
            {
                string message = data.UserID + " Start upload excel list site";

                //blLog.SaveLog(LogAppEnum.StartUploadExcelSite, data.UserID, message);

                SiteBL bl = new SiteBL(DbContext);
                bl.ImportSite(data);

                ImportSiteOutputModel x = new ImportSiteOutputModel();
                x.ImportDate = DateTime.Now;

                res.data     = x;
                res.Message  = "Success upload file excel";
                res.Response = true;

                //blLog.SaveLog(LogAppEnum.EndUploadExcelSite, data.UserID, res.Message);

                return(res);
            }
            catch (Exception ex)
            {
                res.Message  = ex.Message;
                res.Response = false;

                //blLog.SaveLog(LogAppEnum.ErrorUploadExcelSite, data.UserID, ex.Message);

                return(res);
            }
        }
Exemple #2
0
        public IActionResult OnPostImport()
        {
            IFormFile file           = Request.Form.Files[0];
            string    folderName     = "Upload";
            string    folderroot     = "wwwroot";
            string    webRootPath    = "";
            string    errInputFormat = "Mohon check file input anda salah!";
            int       rowNum         = 1;

            if (HttpContext.Session.GetString(SessionKeyDomain) != null && HttpContext.Session.GetString(SessionKeyDomain) != "")
            {
                webRootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            }
            else
            {
                webRootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\")));
            }
            string                 newPath     = Path.Combine(webRootPath, folderroot, folderName);
            StringBuilder          sb          = new StringBuilder();
            ImportSiteModel        model       = null;
            List <ImportSiteModel> listmodel   = new List <ImportSiteModel>();
            ImportSiteInputModel   ImportModel = new ImportSiteInputModel();

            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();
                ISheet sheet;
                string fullPath = Path.Combine(newPath, file.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Position = 0;
                    if (sFileExtension == ".xls")
                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats
                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }
                    else
                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format
                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }
                    IRow headerRow = sheet.GetRow(0);                   //Get Header Row
                    int  cellCount = headerRow.LastCellNum;
                    sb.Append("<table class='table'><tr>");
                    for (int j = 0; j < cellCount; j++)
                    {
                        NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                        sb.Append("<th>" + cell.ToString() + "</th>");
                    }
                    sb.Append("</tr>");
                    sb.AppendLine("<tr>");
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue;
                        }
                        if (row.Cells.All(d => d.CellType == CellType.Blank))
                        {
                            continue;
                        }
                        model = new ImportSiteModel();
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            #region AddtoModel
                            if (j == 0)
                            {
                                model.No = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 1)
                            {
                                model.CP = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 2)
                            {
                                model.KodeFile = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 3)
                            {
                                model.KodeLokasi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 4)
                            {
                                model.ArahLokasi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 5)
                            {
                                model.Pulau = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 6)
                            {
                                model.KodePulau = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 7)
                            {
                                model.Provinsi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 8)
                            {
                                model.KodeProvinsi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 9)
                            {
                                model.Kota = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 10)
                            {
                                model.NamaCabang = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 11)
                            {
                                model.Alamat = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 12)
                            {
                                //type Location
                                model.KelasJalan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 13)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Panjang = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Panjang Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 14)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Lebar = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Lebar Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 15)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Luas = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Luas Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 16)
                            {
                                model.HorV = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 17)
                            {
                                model.Type = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 18)
                            {
                                model.Lampu = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 19)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Qty = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Qty Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 20)
                            {
                                model.Satuan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 21)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.HargaAwal = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Harga Awal Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 22)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.HargaAkhir = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Harga Akhir Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 23)
                            {
                                var minorder = int.Parse(row.GetCell(j).ToString().Split(" ")[0]);
                                var mindasar = row.GetCell(j).ToString().Split(" ")[1];
                                if (minorder > 0 && mindasar.Length > 0)
                                {
                                    if (mindasar.ToLower() == "hari")
                                    {
                                        model.MinimOrder = minorder;
                                        model.MinimDasar = mindasar;
                                    }
                                    else
                                    {
                                        return(this.Content("Satuan hari => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Minimum order harus diisi => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 24)
                            {
                                model.Cabang = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 25)
                            {
                                model.WW = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 26)
                            {
                                model.BID = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 27)
                            {
                                model.NonKop = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 28)
                            {
                                model.Note = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 29)
                            {
                                model.PIC = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 30)
                            {
                                model.Bendera = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 31)
                            {
                                model.ContactAgen = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 32)
                            {
                                model.Telepon = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 33)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Scrore = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Score Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 34)
                            {
                                model.Level = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 35)
                            {
                                model.A = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 36)
                            {
                                model.B = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 37)
                            {
                                model.C = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 38)
                            {
                                model.Keterangan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 39)
                            {
                                model.LinkFolder = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 40)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    if (Double.Parse("-90") <= Double.Parse(row.GetCell(j).ToString()) && Double.Parse("90") >= Double.Parse(row.GetCell(j).ToString()))
                                    {
                                        model.Latitude = row.GetCell(j).ToString().Replace(",", ".");
                                    }
                                    else
                                    {
                                        return(this.Content("Latitude Range -90 s/d 90 => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Latitude Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 41)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    if (Double.Parse("-180") <= Double.Parse(row.GetCell(j).ToString()) && Double.Parse("180") >= Double.Parse(row.GetCell(j).ToString()))
                                    {
                                        model.Longitude = row.GetCell(j).ToString().Replace(",", ".");
                                    }
                                    else
                                    {
                                        return(this.Content("Longitude Range -180 s/d 180 => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Longitude Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            #endregion
                            if (row.GetCell(j) != null)
                            {
                                sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
                            }
                        }
                        sb.AppendLine("</tr>");
                        listmodel.Add(model);
                        rowNum++;
                    }
                    ImportModel.UserID = Guid.Parse(HttpContext.Session.GetString(SessionKeyID));
                    ImportModel.Input  = listmodel;
                    sb.Append("</table>");
                }
                //return this.Content(sb.ToString());
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(BaseAPI + "Site/");
                    client.Timeout     = Timeout.InfiniteTimeSpan;
                    //HTTP POST
                    var postTask = client.PostAsJsonAsync <ImportSiteInputModel>("ImportSite", ImportModel);
                    postTask.Wait();

                    var result = postTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return(this.Content(sb.ToString()));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "ExportImport"));
                    }
                }
            }
            return(this.Content(sb.ToString()));
        }