Create And Export Excel File in Asp.net


 /* IT EXPORT DATA FROM DATA TABLE */


 protected void btnExportfromDt_Click(object sender, EventArgs e)
        {
            string strFilename = "StudDetails.xls";
            Export(LoadData(), strFilename);
        }
        protected void Export(DataTable dtEmp,string filename)
        {
            string attachment = "attachment; filename="+filename;
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;
            foreach (DataColumn dtcol in dtEmp.Columns)
            {
                Response.Write(tab + dtcol.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");
            foreach (DataRow dr in dtEmp.Rows)
            {
                tab = "";
                for (int j = 0; j < dtEmp.Columns.Count; j++)
                {
                    Response.Write(tab + Convert.ToString(dr[j]));
                    tab = "\t";
                }
                Response.Write("\n");
            }
            Response.End();
        }

Note :For Visual Studio 10 use Trigger Tag For Update Panel To Download The File;

1 comment: