I should be getting 580 records but getting none (not even the header) to my excel file.
Button Code
Class Code
Button Code
Code:
protected void btnExport_Click(object sender, EventArgs e)
{
Export2Excel e2e = new Export2Excel();
e2e.Prepare4Excel();
}
Code:
public class Export2Excel
{
public Export2Excel()
{
}
public void Prepare4Excel()
{
string connStr = ConfigurationManager.ConnectionStrings["myCS"].ConnectionString;
string strSQL = "Select * from Employees";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand command = new SqlCommand(strSQL, conn);
SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
BindExcel(ds);
conn.Close();
}
public void BindExcel(DataSet ds)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.AllowPaging = true;
dg.DataBind();
dg.RenderBeginTag(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
Comment