public async Task <IActionResult> OtcPriceList(int productPage = 1) { string path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\externalFiles", "otcPriceList.xlsx"); string url = "https://data.gov.il/dataset/25908429-78f1-450c-8507-20bcfb5a8a22/resource/69d10416-680b-4bc9-900a-62dd9e82430c/download/otc-price-list.xlsx"; List <PrescriptionDrugsPriceListData> prescriptionDrugsPriceListDatas = new List <PrescriptionDrugsPriceListData>(); if (!System.IO.File.Exists(path) || (System.IO.File.Exists(path) && System.IO.File.GetCreationTime(path) < DateTime.Now.AddDays(-7))) { await DownloadNewFile(url, path); } using (var stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read)) { using (var reader = ExcelReaderFactory.CreateReader(stream)) { var result = reader.AsDataSet(); foreach (DataRow row in result.Tables[0].Rows) { int code = 0; if (Int32.TryParse(row[0].ToString(), out code)) { PrescriptionDrugsPriceListData prescriptionDrugsPriceListData = new PrescriptionDrugsPriceListData() { Code = code.ToString(), DrugName = row[1].ToString(), PackageSize = row[2].ToString(), MaximumConsumerPrice = Double.Parse(row[3].ToString() == string.Empty ? "0" : row[3].ToString()), MaximumConsumerPriceIncludingVAT = Double.Parse(row[4].ToString() == string.Empty ? "0" : row[4].ToString()), }; prescriptionDrugsPriceListDatas.Add(prescriptionDrugsPriceListData); } } } } return(View(prescriptionDrugsPriceListDatas)); }
public async Task <IActionResult> PrescriptionDrugsPriceList(int productPage = 1) { string path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\externalFiles", "prescriptiondrugspricelist1.xlsx"); string url = "https://data.gov.il/dataset/76a500c1-f6a9-4276-8606-eccf41962a1f/resource/f40d2fa0-4082-43be-b029-4872d81c8251/download/prescriptiondrugspricelist1.xlsx"; List <PrescriptionDrugsPriceListData> prescriptionDrugsPriceListDatas = new List <PrescriptionDrugsPriceListData>(); if (!System.IO.File.Exists(path) || (System.IO.File.Exists(path) && System.IO.File.GetCreationTime(path) < DateTime.Now.AddDays(-7))) { await DownloadNewFile(url, path); } //using (Stream stream = req.GetResponse().GetResponseStream()) using (var stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read)) { // Auto-detect format, supports: // - Binary Excel files (2.0-2003 format; *.xls) // - OpenXml Excel files (2007 format; *.xlsx) using (var reader = ExcelReaderFactory.CreateReader(stream)) { // Choose one of either 1 or 2: // 1. Use the reader methods //do //{ // while (reader.Read()) // { // // reader.GetDouble(0); // } //} while (reader.NextResult()); // 2. Use the AsDataSet extension method var result = reader.AsDataSet(); foreach (DataRow row in result.Tables[0].Rows) { int code = 0; if (Int32.TryParse(row[0].ToString(), out code)) { PrescriptionDrugsPriceListData prescriptionDrugsPriceListData = new PrescriptionDrugsPriceListData() { Code = code.ToString(), DrugName = row[1].ToString(), PackageSize = row[2].ToString(), MaximumRetailprice = Double.Parse(row[3].ToString() == string.Empty ? "0" : row[3].ToString()), MaximumRetailMargin = row[4].ToString(), MaximumConsumerPrice = Double.Parse(row[5].ToString() == string.Empty ? "0" : row[5].ToString()), MaximumConsumerPriceIncludingVAT = Double.Parse(row[6].ToString() == string.Empty ? "0" : row[6].ToString()), CodeWillHeal = row[7].ToString(), CodePharmaSoft = row[8].ToString() }; prescriptionDrugsPriceListDatas.Add(prescriptionDrugsPriceListData); } } // The result of each spreadsheet is in result.Tables } } int count = prescriptionDrugsPriceListDatas.Count; PrescriptionDrugsPriceListDataViewModel prescriptionDrugsPriceListDataViewModel = new PrescriptionDrugsPriceListDataViewModel() { PrescriptionDrugsPriceListDatas = prescriptionDrugsPriceListDatas .Skip((productPage - 1) * PageSize) .Take(PageSize).ToList(), PagingInfo = new PagingInfo() { CurrentPage = productPage, ItemsPerPage = PageSize, TotalItem = count } }; return(View(prescriptionDrugsPriceListDataViewModel)); }