private void ValidarDUESSiscomex(List <DUEMaster> dues)
        {
            this.gvNotasFiscais.DataSource = dues.ToList();
            this.gvNotasFiscais.DataBind();

            foreach (GridViewRow linhaGrid in this.gvNotasFiscais.Rows)
            {
                var due = linhaGrid.Cells[0].Text.Replace(";", "");

                var dadosDue = ServicoSiscomex2.ObterDetalhesDUE(due, ConfigurationManager.AppSettings["CpfCertificado"].ToString()).Result;

                if (dadosDue.Sucesso == false)
                {
                    linhaGrid.Cells[1].Text = string.Empty;
                    linhaGrid.Cells[2].Text = string.Empty;
                    linhaGrid.Cells[3].Text = dadosDue.Mensagem;

                    linhaGrid.BackColor = System.Drawing.Color.MistyRose;
                }
                else
                {
                    var situacao = _situacaoDUEDAO.ObterSituacoesDUE()
                                   .Where(c => c.Id == dadosDue.situacaoDUE).FirstOrDefault();

                    if (situacao != null)
                    {
                        linhaGrid.Cells[2].Text = situacao.Descricao;
                    }
                    else
                    {
                        linhaGrid.Cells[2].Text = string.Empty;
                    }

                    linhaGrid.Cells[1].Text = dadosDue.dataSituacaoDUE;
                }
            }
        }
        protected void btnGerarExcel_Click(object sender, EventArgs e)
        {
            var dues = new List <DUEMaster>();

            foreach (GridViewRow linhaGrid in this.gvNotasFiscais.Rows)
            {
                var due = linhaGrid.Cells[0].Text.Replace(";", "");

                var dadosDue = ServicoSiscomex2.ObterDetalhesDUE(due, ConfigurationManager.AppSettings["CpfCertificado"].ToString()).Result;

                if (dadosDue.Sucesso == false)
                {
                    linhaGrid.Cells[1].Text = string.Empty;
                    linhaGrid.Cells[2].Text = string.Empty;
                    linhaGrid.Cells[3].Text = dadosDue.Mensagem;

                    linhaGrid.BackColor = System.Drawing.Color.MistyRose;

                    dues.Add(new DUEMaster
                    {
                        DUE = due,
                        DescricaoSituacao = string.Empty,
                        DataSituacaoDUE   = string.Empty,
                        StatusSiscomex    = dadosDue.Mensagem
                    });
                }
                else
                {
                    var situacao = _situacaoDUEDAO.ObterSituacoesDUE()
                                   .Where(c => c.Id == dadosDue.situacaoDUE).FirstOrDefault();

                    if (situacao != null)
                    {
                        linhaGrid.Cells[2].Text = situacao.Descricao;
                    }
                    else
                    {
                        linhaGrid.Cells[2].Text = string.Empty;
                    }

                    linhaGrid.Cells[1].Text = dadosDue.dataSituacaoDUE;

                    dues.Add(new DUEMaster
                    {
                        DUE = due,
                        DescricaoSituacao = linhaGrid.Cells[2].Text,
                        DataSituacaoDUE   = linhaGrid.Cells[1].Text,
                        StatusSiscomex    = string.Empty
                    });
                }
            }

            ExcelPackage   epackage = new ExcelPackage();
            ExcelWorksheet excel    = epackage.Workbook.Worksheets.Add("CCT");

            excel.Cells["A1"].LoadFromCollection(dues.Select(c => new
            {
                c.DUE,
                c.DescricaoSituacao,
                c.DataSituacaoDUE,
                c.StatusSiscomex
            }), true);

            string attachment = "attachment; filename=ListaDUEs.xlsx";

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", attachment);
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            HttpContext.Current.Response.BinaryWrite(epackage.GetAsByteArray());

            HttpContext.Current.Response.End();
            epackage.Dispose();
        }