Example #1
0
        public void ShouldWriteFile()
        {
            var writer = new SLExcelWriter();
            var result = writer.GenerateExcel(new SLExcelData
            {
                Headers = new List <string> {
                    "F1", "F2", "F3"
                },
                DataRows = new List <List <string> >
                {
                    new List <string> {
                        "F1 1", "F2 1", "F3 1"
                    },
                    new List <string> {
                        "F1 2", "F2 2", "F3 2"
                    },
                    new List <string> {
                        "F1 3", "F2 3", "F3 3"
                    },
                }
            });

            File.WriteAllBytes("Excel.Output.xlsx", result);
        }
        private async void Save(Object o)
        {
            IsBusy = true;
            try
            {
                var token = (Application.Current as App).Settings.AccessToken;

                // Save settings
                (Application.Current as App).Settings.PostUrl  = PostUrl;
                (Application.Current as App).Settings.FilePath = Path;

                Progress = "Getting Post Id...";

                var postId = await FacebookHelper.GetPostIdFromUrl(token, PostUrl);

                if (postId == null)
                {
                    MessageBox.Show("Could not get the post/page Id");
                    IsBusy = false;
                    return;
                }

                _tokenSource.Token.ThrowIfCancellationRequested();
                Progress = "Downloading comments...";

                var    list          = new List <Comment>();
                int    totalComments = 1;
                string from          = null;
                string after         = null;
                do
                {
                    var pagedList = await FacebookHelper.GetComments(token, postId, from, after).ConfigureAwait(false);

                    if (pagedList == null)
                    {
                        throw new InvalidDataException();
                    }
                    if (pagedList.Children.Count == 0)
                    {
                        break;
                    }

                    totalComments = pagedList.Summary.TotalCount;
                    after         = pagedList.Paging.Cursors.After;
                    list.AddRange(pagedList.Children);

                    _tokenSource.Token.ThrowIfCancellationRequested();
                    Progress = $"Downloading comments ({list.Count} of {totalComments})...";
                } while (list.Count < totalComments);

                _tokenSource.Token.ThrowIfCancellationRequested();
                Progress = "Saving Excel file...";

                SLExcelData data = GetNewDataObject();
                foreach (var comment in list)
                {
                    data.DataRows.Add(new List <string>()
                    {
                        comment.From.Id, comment.From.Name,
                        comment.CreatedTime.ToString("g"), comment.Message, FacebookHelper.GetVotes(comment.Message)
                    });
                }

                byte[] file = SLExcelWriter.GenerateExcel(data);
                using (var fileStream = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
                {
                    fileStream.SetLength(file.Length);
                    await fileStream.WriteAsync(file, 0, file.Length);
                }

                Process.Start(Path);
            }
            catch (OperationCanceledException)
            {
                _tokenSource = new CancellationTokenSource();
            }
            catch (InvalidDataException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }