private void retrieveDataFromExcel() { try{ Excel.Worksheet xlWorksheet = (Excel.Worksheet)workbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count; int columnCount = xlRange.Columns.Count; // String emailHeader =null, empIDHeader =null; for (int i = 1; i < rowCount + 1; i++) { SortedList fieldMap = new SortedList(); for (int j = 1; j < columnCount + 1; j++) { object rangeObject = xlWorksheet.Cells[i, j]; Excel.Range range = (Excel.Range)rangeObject; Object rangeValue = range.Value2; String cellValue; try { cellValue = rangeValue.ToString(); }catch (NullReferenceException) { cellValue = String.Empty; } if (i == 1) { Headers.Add(cellValue); } else { fieldMap.Add(Headers[j - 1], cellValue); } } if (i != 1) { Recepients.Add(new Recepient(fieldMap)); } } }catch (Exception ex) { MessageBox.Show(ex.Message, "Error while reading Excel file", MessageBoxButtons.OK, MessageBoxIcon.Error); ErrorFree = false; } finally{ workbook.Close(); } }
/// <summary> /// Добавление получателя. /// </summary> /// <param name="address"> Email получателя. </param> /// <returns> Сообщение. </returns> public EmailMessage AddRecepient(string address) { Recepients.Add(address); return(this); }