コード例 #1
0
        protected void btnGerarExcelParcial_Click(object sender, EventArgs e)
        {
            var guid = hddnGUID.Value;

            if (string.IsNullOrEmpty(guid))
            {
                ModelState.AddModelError(string.Empty, "Ocorreu um problema ao exportar arquivo");
            }

            if (!ModelState.IsValid)
            {
                return;
            }

            var _notaFiscalDAO = new NotaFiscalDAO();

            var notas = _notaFiscalDAO.ObterNFsImportadasPorGUID(guid);

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

            excel.Cells["A1"].LoadFromCollection(notas.Select(c => new
            {
                c.DataRegistro,
                c.ChaveNF,
                c.SaldoCCT,
                c.PesoEntradaCCT,
                c.PesoAferido,
                c.Observacoes,
                c.Recinto,
                c.UnidadeReceita,
                c.Item,
                c.DUE,
                c.QtdeAverbada
            }), true);

            string attachment = $"attachment; filename=ArquivoNotas.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();
        }