public void ExportBibleNotesToExcel( BackupFile backupFile, string bibleNotesExportFilePath, IExcelService excelService) { File.Delete(bibleNotesExportFilePath); if (File.Exists(bibleNotesExportFilePath)) { throw new BackupFileServicesException( $"Could not delete existing Excel file: {bibleNotesExportFilePath}"); } var notesToWrite = new List <ExcelServices.Models.BibleNote>(); var tags = backupFile.Database.TagMaps.Where(x => x.NoteId != null).ToLookup(map => map.NoteId, map => map); foreach (var note in backupFile.Database.Notes) { if (note.LocationId == null) { continue; } var location = backupFile.Database.FindLocation(note.LocationId.Value); if (location?.BookNumber == null) { continue; } int?colorCode = null; if (note.UserMarkId != null) { var userMark = backupFile.Database.FindUserMark(note.UserMarkId.Value); if (userMark != null) { colorCode = userMark.ColorIndex; } } notesToWrite.Add(new ExcelServices.Models.BibleNote { BookNumber = location.BookNumber.Value, BookName = BibleBookNames.GetName(location.BookNumber.Value), ChapterNumber = location.ChapterNumber, VerseNumber = note.BlockIdentifier, NoteTitle = note.Title?.Trim(), NoteContent = note.Content?.Trim(), PubSymbol = location.KeySymbol, ColorCode = colorCode, TagsCsv = GetTagsAsCsv(tags, note.NoteId, backupFile.Database), }); } notesToWrite.Sort(SortBibleNotes); excelService.AppendToBibleNotesFile(bibleNotesExportFilePath, notesToWrite, 0, bibleNotesExportFilePath); }