using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.Utility.Extensions; using SpreadsheetLight; namespace Sony.Filtr.Utility { public static class ExcelFileUtility { public static string GetFilenameDatePart() { return DateTime.Today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture); } public static Stream GetExcelStream(List playlistAttributes) { MemoryStream stream = new MemoryStream(); var doc = new SLDocument(); doc.SetCellValue(1, 1, "Name"); doc.SetCellValue(1, 2, "SpotifyUri"); doc.SetCellValue(1, 3, "Subscribers"); doc.SetCellValue(1, 4, "Subscribers7"); doc.SetCellValue(1, 5, "Subscribers30"); doc.SetCellValue(1, 6, "Active"); doc.SetCellValue(1, 7, "Priority"); foreach (var playlistAttribute in playlistAttributes.ItemIndex()) { var excelRowIndex = playlistAttribute.Index + 2; var playlist = playlistAttribute.Item.EditorialPlaylist; doc.SetCellValue(excelRowIndex, 1, playlist.Name); doc.SetCellValue(excelRowIndex, 2, playlist.SpotifyLinkUri); if (playlistAttribute.Item.Attributes.Followers.HasValue) doc.SetCellValue(excelRowIndex, 3, playlistAttribute.Item.Attributes.Followers.Value); if (playlistAttribute.Item.Attributes.FollowerChange7.HasValue) doc.SetCellValue(excelRowIndex, 4, playlistAttribute.Item.Attributes.FollowerChange7.Value); if (playlistAttribute.Item.Attributes.FollowerChange30.HasValue) doc.SetCellValue(excelRowIndex, 5, playlistAttribute.Item.Attributes.FollowerChange30.Value); doc.SetCellValue(excelRowIndex, 6, playlist.Active.ToString()); doc.SetCellValue(excelRowIndex, 7, playlist.Priority); } doc.SaveAs(stream); stream.Position = 0; return stream; } public static Stream GetExcelStreamForPlaylistStats(Dictionary statistics) { MemoryStream stream = new MemoryStream(); var sl = new SLDocument(); sl.SetCellValue(1, 1, "Date"); sl.SetCellValue(1, 2, "Subscribers"); sl.SetCellValue(1, 3, "Change"); uint? prev = null; foreach (var stat in statistics.ItemIndex().ToList()) { var excelRowIndex = stat.Index + 2; sl.SetCellValue(excelRowIndex, 1, stat.Item.Key); sl.SetCellStyle(excelRowIndex, 1, new SLStyle() { FormatCode = "yyyy-mm-dd" }); if (stat.Item.Value.HasValue) { sl.SetCellValue(excelRowIndex, 2, stat.Item.Value.Value); if (prev.HasValue) { sl.SetCellValue(excelRowIndex, 3, (int)stat.Item.Value - (int)prev); } prev = stat.Item.Value.Value; } } sl.SaveAs(stream); stream.Position = 0; return stream; } public static Stream GetExcelStream(List browseItems, DateTime date) { MemoryStream stream = new MemoryStream(); var doc = new SLDocument(); doc.SetCellValue(1, 1, "Market"); doc.SetCellValue(1, 2, "Date"); doc.SetCellValue(1, 3, "Category"); doc.SetCellValue(1, 4, "Position"); doc.SetCellValue(1, 5, "Peak"); doc.SetCellValue(1, 6, "URI"); doc.SetCellValue(1, 7, "Username"); doc.SetCellValue(1, 8, "Name"); doc.SetCellValue(1, 9, "Owner"); doc.SetCellValue(1, 10, "SME Owner Market"); doc.SetCellValue(1, 11, "Users"); doc.SetCellValue(1, 12, "Streams"); doc.SetCellValue(1, 13, "Followers"); foreach (var browseItem in browseItems.ItemIndex()) { var index = browseItem.Index + 2; doc.SetCellValue(index, 1, browseItem.Item.BrowseMarket); doc.SetCellValue(index, 2, date); doc.SetCellStyle(index, 2, new SLStyle() { FormatCode = "yyyy-mm-dd" }); doc.SetCellValue(index, 3, browseItem.Item.CategoryName); doc.SetCellValue(index, 4, browseItem.Item.Position); doc.SetCellValue(index, 5, browseItem.Item.Peak); doc.SetCellValue(index, 6, browseItem.Item.PlaylistURI); doc.SetCellValue(index, 7, browseItem.Item.OwnerUsername); doc.SetCellValue(index, 8, browseItem.Item.PlaylistName); doc.SetCellValue(index, 9, browseItem.Item.BuzzCategoryName); doc.SetCellValue(index, 10, browseItem.Item.PlaylistMarket); if (browseItem.Item.UniqueUserStreams.HasValue) doc.SetCellValue(index, 11, browseItem.Item.UniqueUserStreams.Value); if (browseItem.Item.TotalStreams.HasValue) doc.SetCellValue(index, 12, browseItem.Item.TotalStreams.Value); if (browseItem.Item.Followers.HasValue) doc.SetCellValue(index, 13, browseItem.Item.Followers.Value); } doc.SaveAs(stream); stream.Position = 0; return stream; } } }