private void Export_To_Excel(DataTable dtTest, String strFileName, String strFileHeader, String strFromDate, String strToDate, int AbandonmentRate) { try { if (dtTest.Rows.Count > 0) { string attachment = "attachment; filename=" + strFileName + "_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_tt") + ".xls"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel"; String strLine = "------------------------------"; String strLineCm = ""; //------------------- //int cntDtRows = dtTest.Columns.Count - 1; int cntDtRows = dtTest.Columns.Count; //cntDtRows--; String[] arrHeader = new String[20]; for (int p = 0; p < cntDtRows; p++) { arrHeader[p] = dtTest.Columns[p].ColumnName; strLineCm = strLineCm + strLine; } //---------------------- Response.Write("<table width=" + (cntDtRows * 150) + " align='left' valign='top'>"); Response.Write("<tr><td colspan='" + cntDtRows + "' align=center><h2><u>" + strFileHeader + "</u></h2></td></tr>"); Response.Write("<tr><td colspan='" + cntDtRows + "' align=center>(From Date " + strFromDate + " To Date " + strToDate + ")</td></tr>"); //Response.Write("<tr><td align='right' colspan='" + cntDtRows + "'>Abandonment Rate( " + AbandonmentRate.ToString() + "% )</td></tr>"); Response.Write("<tr><td colspan='" + cntDtRows + "'>" + strLineCm + "</td></tr>"); Response.Write("<tr>"); for (int i = 0; i < cntDtRows; i++) { Response.Write("<td valign='top' style='background-color:green; color:white;' width='150' align=left><b><font size='2' face='verdana'>" + arrHeader[i].ToString() + "</font></b></td>"); } Response.Write("</tr>"); Response.Write("<tr><td colspan='" + cntDtRows + "'>" + strLineCm + "</td></tr>"); foreach (DataRow dRow in dtTest.Rows) { Response.Write("<tr>"); int cntCol = 0; foreach (object obj in dRow.ItemArray) { if (cntCol == cntDtRows) { break; } if (cntCol == 1 || cntCol == 5 || cntCol == 6) { if (obj.ToString().Length > 0) { Response.Write("<td valign='top' valign='top' width='150' align=left>'" + obj.ToString() + "</td>"); } else { Response.Write("<td valign='top' valign='top' width='150' align=left>" + obj.ToString() + "</td>"); } } else { Response.Write("<td valign='top' valign='top' width='150' align=left>" + obj.ToString() + "</td>"); } // Response.Write("<td valign='top' width='150' align=left>" + obj.ToString() + "</td>"); cntCol++; } Response.Write("</tr>"); } Response.Write("</table>"); Response.End(); } } catch (Exception ex) { } }
Response.End
HttpContext.Current.ApplicationInstance.CompleteRequest
Application_EndRequest
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)