In Asp.Net some time we need to export datatable into Excel file format for reporting purpose. For full this requirement, we can convert the data into excel very easily. For that, we first create a Datatable as below:
  1. public DataTable CreateTable()
  2. {
  3. DataTable dt = new DataTable();
  4. dt.Columns.Add("EmployeeCode", typeof(string));
  5. dt.Columns.Add("EmployeeName", typeof(string));
  6. dt.Columns.Add("Address", typeof(string));
  7. dt.Columns.Add("City", typeof(string));
  8. dt.Columns.Add("PinCode", typeof(Int32));
  9. dt.Columns.Add("PhoneNo", typeof(string));
  10. return dt;
  11. }
After it, we add some data into this data table. And After it, we want to export this data into on click of a Button. Against click event, we call the function ExportToExcel as below:
  1. private void ExportToExcel(DataTable dtExcel)
  2. {
  3. try
  4. {
  5. HttpContext.Current.Response.Clear();
  6. HttpContext.Current.Response.ClearContent();
  7. HttpContext.Current.Response.ClearHeaders();
  8. HttpContext.Current.Response.Buffer = true;
  9. HttpContext.Current.Response.ContentType = "application/ms-excel";
  10. HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
  11. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=Salary_Cert.xls");
  12. HttpContext.Current.Response.Charset = "utf-8";
  13. HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
  14. HttpContext.Current.Response.Write("<font style='font-size:11.0pt; font-family:Calibri;'>");
  15. HttpContext.Current.Response.Write("<BR><BR>");
  16. HttpContext.Current.Response.Write("<Table border='0' bgColor='#ffffff' " +
  17. "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
  18. "style='font-size:11.0pt; font-family:Calibri; background:white;'>");
  19. #region Report Header
  20. HttpContext.Current.Response.Write("<TR valign='top'>");
  21. HttpContext.Current.Response.Write("<B><U><TD align='center' colspan='9' style='font-size:14.0pt;text-weight:bold;text-decoration:underline;'>TO WHOMSOEVER IT MAY CONCERN</TD>");
  22. HttpContext.Current.Response.Write("</U></B></TR>");
  23. HttpContext.Current.Response.Write("<TR valign='top'><TD align='left' colspan='9'> Employee Personal Details </TD></TR>");
  24. HttpContext.Current.Response.Write("<TR valign='top'><TD align='left' colspan='9'> </TD></TR>");
  25. HttpContext.Current.Response.Write("<TR valign='top'><TD align='left' colspan='9' rowspan='3' style='whitespace:normal;'>");
  26. HttpContext.Current.Response.Write("</TD></TR>");
  27. #endregion
  28. #region Header Row
  29. HttpContext.Current.Response.Write("<TR valign='top'><td colspan='10'");
  30. HttpContext.Current.Response.Write("<Table border='1' bgColor='#FFFFFF' " +
  31. "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
  32. "style='font-size:10.0pt; font-family:Calibri; background:white;'>");
  33. HttpContext.Current.Response.Write("<TR valign='top' style='background:#D8D8D8;'>");
  34. HttpContext.Current.Response.Write("<TD align='left' style='width:20%;'>Employee Code</TD>");
  35. HttpContext.Current.Response.Write("<TD align='center' style='width:10%;'>Employee Name</TD>");
  36. HttpContext.Current.Response.Write("<TD align='center' style='width:10%;'>Address</TD>");
  37. HttpContext.Current.Response.Write("<TD align='center' style='width:10%;'>City</TD>");
  38. HttpContext.Current.Response.Write("<TD align='center' style='width:10%;'>Pin Code</TD>");
  39. HttpContext.Current.Response.Write("<TD align='center' style='width:10%;'>Phone No</TD>");
  40. HttpContext.Current.Response.Write("</TR>");
  41. #endregion
  42. #region Detail Row
  43. for (int iRow = 0; iRow < dtExcel.Rows.Count; iRow++)
  44. {
  45. HttpContext.Current.Response.Write("<TR valign='top'>");
  46. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["EmployeeCode"].ToString() + "</TD>");
  47. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["EmployeeName"].ToString() + "</TD>");
  48. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["Address"].ToString() + "</TD>");
  49. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["City"].ToString() + "</TD>");
  50. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["PinCode"].ToString() + "</TD>");
  51. HttpContext.Current.Response.Write("<TD align='left'>" + dtExcel.Rows[iRow]["PhoneNo"].ToString() + "</TD>");
  52. HttpContext.Current.Response.Write("</TR>");
  53. }
  54. HttpContext.Current.Response.Write("</Table>");
  55. #endregion
  56. HttpContext.Current.Response.Write("</Table>");
  57. HttpContext.Current.Response.Write("</font>");
  58. HttpContext.Current.Response.Flush();
  59. HttpContext.Current.Response.End();
  60. }
  61. catch (Exception ex)
  62. {
  63. throw (ex);
  64. }
  65. }