private void mnbDownload_Click(object sender, RibbonControlEventArgs e) { //Check MNB service if (_mnbService.State.ToString() != "Created") { _mnbService = new MNBArfolyamServiceSoapClient(); _mnbService.Open(); } //Log to the database LogDatabaseDataSet.LogRow newLogRow = _logDatabaseDataSet.Log.NewLogRow(); newLogRow.Név = Environment.UserName; newLogRow.TimeStamp = DateTime.Now; _logDatabaseDataSet.Log.Rows.Add(newLogRow); _logTableAdapter.Update(_logDatabaseDataSet.Log); //Get currencies xml and map it to its model GetCurrenciesResponseBody currenciesXML = _mnbService.GetCurrencies(new GetCurrenciesRequestBody()); MNBCurrencies currenciesModel = (MNBCurrencies)_operatorService.XmlToModel <MNBCurrencies>(currenciesXML.GetCurrenciesResult); //Exchange rates query config GetExchangeRatesRequestBody getExchangeRatesRequestBody = new GetExchangeRatesRequestBody { startDate = "2015.01.01.", endDate = "2020.04.01.", currencyNames = string.Join(",", currenciesModel.Currencies) }; //Get exchange rates xml and map it to its model GetExchangeRatesResponseBody exchangeRatesXML = _mnbService.GetExchangeRates(getExchangeRatesRequestBody); MNBExchangeRates exchangeRatesModel = (MNBExchangeRates)_operatorService.XmlToModel <MNBExchangeRates>(exchangeRatesXML.GetExchangeRatesResult); //Create datatable from model and import it to Excel then save it DataTable dataTable = _operatorService.ModelToDataTable(exchangeRatesModel, currenciesModel); DataSet dataSet = new DataSet(); dataSet.Tables.Add(dataTable); _operatorService.DataSetToExcel(dataSet); //Format worksheet and save the workbook to the user's documents Excel.Worksheet activeWorksheet = Globals.ThisAddIn.Application.ActiveSheet; activeWorksheet.Range["A2:CV2"].NumberFormatLocal = ""; activeWorksheet.Range["B3:CV10000"].NumberFormatLocal = "0"; activeWorksheet.Range["A3:A10000"].NumberFormatLocal = "éééé\\.hh\\.nn\\."; string savePath = Directory.GetCurrentDirectory() + "\\arfolyam-letoltes.xlsx"; Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs(savePath); _mnbService.Close(); }
/// <summary> /// Query the MNB Client to retrieve Exchange rates for given currencies /// </summary> /// <param name="start">DateTime: The start date to be used in the query</param> /// <param name="end">DateTime: The end date to be used in the query</param> /// <param name="currencyNames">IList<string>: The names of the currencies to be used in the query</param> private void GetAndProcessExchangeRates(DateTime start, DateTime end, IList <string> currencyNames) { try { string startDate = start.ToString(DATE_REQUEST_FORMAT); string endDate = end.ToString(DATE_REQUEST_FORMAT); var currencies = string.Join(",", currencyNames); GetExchangeRatesRequestBody requestBody = new GetExchangeRatesRequestBody() { startDate = startDate, endDate = endDate, currencyNames = currencies }; GetExchangeRatesResponseBody exchangeRatesResponseBody = client.GetExchangeRates(requestBody); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(exchangeRatesResponseBody.GetExchangeRatesResult); XmlNodeList rawNodes = xmlDoc.SelectNodes(MNB_EXCHANGE_RATES_PER_DAY); foreach (XmlNode rawNode in rawNodes) { // Retrieve the day of the FX rate and store it in variable: date XmlNode rawDate = rawNode.Attributes.GetNamedItem(MNB_DATE_ATTRIBUTE_NAME); DateTime.TryParse(rawDate.Value, out DateTime date); // check if date is not included var dateIsAlreadyAdded = CurrencySnapshotDate.Any(item => item.Date == date); if (!dateIsAlreadyAdded) { var newFxDate = new FxDateWrapper() { Date = date, }; CurrencySnapshotDate.Add(newFxDate); } // Iterate through child nodes and retrieve the unit and curr from attributes - and fx rate from innerText foreach (XmlNode currencyRate in rawNode.ChildNodes) { // Find currency by name XmlNode currAttrib = currencyRate.Attributes.GetNamedItem(MNB_CURRENCY_ATTRIBUTE_NAME); var currencyWrapper = CurrenciesRetrieved.FirstOrDefault(currName => currName.CurrencyName.Equals(currAttrib.Value, StringComparison.InvariantCultureIgnoreCase)); if (currencyWrapper is null) { continue; } // feed currency unit to currencyWrapper XmlNode unitAttrib = currencyRate.Attributes.GetNamedItem(MNB_UNIT_ATTRIBUTE_NAME); if (Int32.TryParse(unitAttrib.Value, out int result)) { currencyWrapper.RateUnit = result; } // Feed rate into currencyWrapper currencyWrapper.CurrencyRates.Add(new Currency() { Date = date, RawRate = currencyRate.InnerText }); } } } catch (Exception e) { string error = e.Message; ShowErrorMessage(error); } }