Thursday 28 August 2014

Export Datatable value to Excel in asp.net c#

Hi Everyone in this article  have shown how to export data table value in excel .
basically I don't have ms office installed so i  have opened with browser.
basically that is html table i  am generating but because i have given .xls extension so we can open it in
excel also.

so whatever e column u have included in stored procedure will come in report.
just pass data table value or rows and any file name so it will take all rows from table and convert them into
html format so we can open in excel also.
lets have a look.

 On buttonclick it will export
protected void btnExport_Click(Object sender, EventArgs e)
    {
     
        ExportDataToExcelUsingDataTable((DataTable)ViewState["table"], "Report For User" + DateTime.Now.ToShortDateString() + ".xls");

    }


function to convert datatable record or data to excel format like 
public void ExportDataToExcelUsingDataTable(DataTable DtRecordOfUser, string FlNm)
    {
        try
        {
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + FlNm);
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            string tab = string.Empty;

            HttpContext.Current.Response.Write("<table width='100%' border='1' " + "cellSpacing='0' cellPadding='0' " +


"style='font-size:11.0pt;'> <tr style='font-size:12pt;'>");


            for (int j = 0; j < DtRecordOfUser.Columns.Count; j++)

            {

                HttpContext.Current.Response.Write("<td  style='background-color:#339999;color:#FFF878;'>");

                HttpContext.Current.Response.Write("<B>");
                HttpContext.Current.Response.Write(DtRecordOfUser.Columns[j].ColumnName.ToString());
                HttpContext.Current.Response.Write("</B>");
                HttpContext.Current.Response.Write("</td>");
            }
            HttpContext.Current.Response.Write("</tr>");
            foreach (DataRow row in DtRecordOfUser.Rows)
            {

                HttpContext.Current.Response.Write("<tr>");

                for (int i = 0; i < DtRecordOfUser.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<td  style='background-color:#FFFBD6;color:#333333;'>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</td>");
                }

                HttpContext.Current.Response.Write("</tr>");

            }
            

            HttpContext.Current.Response.Write("</table>");

            HttpContext.Current.Response.Write("</font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
        catch (Exception ex)
        {
            
        }

    }



export to excel using table



Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: