Categorias

ASP.NET: Exportanto para Excel com mais desempenho

Pessoal,

Estive pesquisando e pode constatar que é muito melhor, em relação a performace armazenar os dados em uma sessão e só daí então exportar para excel com ASP.NET.

Estou disponibilizando o código fonte e caso tenha alguma dúvida, basta dizer!

Abraços.

using System.Data;
using System.Data.SqlClient;

namespace ExportaExcel
{
    public class ObterDados
    {
        #region Métodos

        public DataTable Obter()
        {
            string ConnectionString = @"Data Source=NTAPELUSOSQLEXPRESS;User Id=usuario;Password=and@123;Initial Catalog=CODIGOFONTE;";

            DataTable lObjDataTable = new DataTable();

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                using (SqlCommand lObjSqlCommand = new SqlCommand())
                {
                    lObjSqlCommand.Connection = connection;
                    lObjSqlCommand.CommandText = "SELECT * FROM COLABORADOR";

                    using (SqlDataAdapter lObjDataAdpter = new SqlDataAdapter())
                    {
                        lObjDataAdpter.SelectCommand = lObjSqlCommand;
                        lObjDataAdpter.Fill(lObjDataTable);
                    }
                }
            }
            return lObjDataTable;
        }
        #endregion
    }
}

Arquivos para Download:

ExportaExcel.rar