Hi nedash,
Refer the below sample code without using any dll.
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd")));
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dataColumn in dt.Columns)
{
Response.Write(tab + dataColumn.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dataRow in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dataRow[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Response.ClearContent()
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd")))
Response.ContentType = "application/vnd.ms-excel"
Dim tab As String = ""
For Each dataColumn As DataColumn In dt.Columns
Response.Write(tab + dataColumn.ColumnName)
tab = vbTab
Next
Response.Write(vbLf)
Dim i As Integer
For Each dataRow As DataRow In dt.Rows
tab = ""
For i = 0 To dt.Columns.Count - 1
Response.Write(tab & dataRow(i).ToString())
tab = vbTab
Next
Response.Write(vbLf)
Next
Response.[End]()
End Sub
Below is another way.
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
DataTableToExcel(dt);
}
public void DataTableToExcel(DataTable dt)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.Charset = Encoding.UTF8.WebName;
response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
response.AddHeader("Content-Type", "application/Excel");
response.ContentType = "application/vnd.xlsx";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
GridView gridView = new GridView();
gridView.DataSource = dt;
gridView.DataBind();
gridView.RenderControl(htw);
response.Write(sw.ToString());
gridView.Dispose();
dt.Dispose();
response.End();
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
DataTableToExcel(dt)
End Sub
Public Sub DataTableToExcel(dt As DataTable)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.ClearHeaders()
response.ClearContent()
response.Charset = Encoding.UTF8.WebName
response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls")
response.AddHeader("Content-Type", "application/Excel")
response.ContentType = "application/vnd.xlsx"
Using sw As New StringWriter()
Using htw As New HtmlTextWriter(sw)
Dim gridView As New GridView()
gridView.DataSource = dt
gridView.DataBind()
gridView.RenderControl(htw)
response.Write(sw.ToString())
gridView.Dispose()
dt.Dispose()
response.[End]()
End Using
End Using
End Sub