public static List <Contact> ReadExcel(string filename) { Excel.Application excel = null; Excel.Workbook workbook = null; try { excel = new Excel.Application(); excel.Visible = false; workbook = excel.Workbooks.Open(filename, ReadOnly: true); Excel.Worksheet sheet = workbook.Sheets[1]; Excel.ListObject table = sheet.ListObjects["Information_Table"]; List <ProtoContact> contacts = new List <ProtoContact>(); foreach (Excel.ListRow row in table.ListRows) { ProtoContact c = new ProtoContact(table.ListColumns, row); contacts.Add(c); } Dictionary <string, bool> multiple = contacts.GroupBy(c => c.First).ToDictionary(cg => cg.Key, cg => cg.Count() > 1); return(contacts.Select(c => { string shortName = multiple[c.First] ? c.First + " " + c.Last.Substring(0, 1) + "." : c.First; return new Contact(c, shortName); }).ToList()); } finally { if (workbook != null) { workbook.Close(); } if (excel != null) { excel.Quit(); } } }
internal Contact(ProtoContact pc, string shortName) { this.First = pc.First; this.Last = pc.Last; this.Birthday = pc.Birthday; this.HomePhone = pc.HomePhone; this.CellPhone = pc.CellPhone; this.Email = pc.Email; this.WorkEmail = pc.WorkEmail; this.Address1 = pc.Address1; this.Address2 = pc.Address2; this.Address = pc.Address; this.ShortName = shortName; }