ExcelReference xlSelection = XlCall.Excel(XlCall.xlfSelection) as ExcelReference;
string selectionAddress = (string)XlCall.Excel(XlCall.xlfReftext, xlSelection, true);
ExcelReference sel = new ExcelReference(xlSelection.RowFirst, xlSelection.RowLast-1, xlSelection.ColumnFirst, xlSelection.ColumnLast);
object[,] xlContents = sel.GetValue() as object[,];
List<object> colContents = xlContents.Cast<object>().ToList();
colContents.RemoveAll(c => c == ExcelDna.Integration.ExcelEmpty.Value);
if (xlSelection == null)
MessageBox.Show("No current selection");
int currentCol = Convert.ToInt16((XlCall.Excel(XlCall.xlfColumn) as object[,])[0, 0]);
int newColNum = xlSelection.ColumnLast + 1;
int firstRow = xlSelection.RowFirst;
int lastRow = colContents.Count - 1;
ExcelReference newSelection = new ExcelReference(firstRow, lastRow, newColNum, newColNum);
XlCall.Excel(XlCall.xlcSelect, newSelection);
XlCall.Excel(XlCall.xlcInsert, 4);
List<string> subcity = new List<string>();
// get the DB table to compare the selection with
using (myEntities db = new myEntities()) {
var suburbs = db.OLDPcodes_SuburbList;
foreach (var cont in xlContents) {
string content = string.Empty;
if (cont != ExcelDna.Integration.ExcelEmpty.Value)
content = cont as string;
else
continue;
var found = suburbs.Where(s => ((s.Place + " " + s.District) == content)).FirstOrDefault();
string newColValue = string.Empty;
if (found != null) {
newColValue = found.Place + " | " + found.District;
subcity.Add(newColValue);
Debug.WriteLine(newColValue);
}
}
}
string[,] arr = new string[subcity.ToArray().Length, 1];
for (int r = 0; r <= subcity.ToArray().Length-1; r++) {
arr[r, 0] = subcity[r];
}
// output the list to the new column
newSelection.SetValue(arr);