Hi nauna,
Refer below example.
For export to excel i have used ClosedXML. You can refer below article.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespaces
C#
using System.Data;
using System.Linq;
using System.Reflection;
VB.Net
Imports System.Data
Imports System.Linq
Imports System.Reflection
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
NorthwindEntities db = new NorthwindEntities();
var result = (from c in db.Customers
select new
{
Id = c.CustomerID,
Name = c.ContactName
}).ToList();
DataTable dt = ToDataTable(result);
using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook())
{
wb.Worksheets.Add(dt, "Customers");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Customers.xlsx");
using (System.IO.MemoryStream MyMemoryStream = new System.IO.MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable(typeof(T).Name);
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
dataTable.Columns.Add(prop.Name);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim db As NorthwindEntities = New NorthwindEntities()
Dim result = (From c In db.Customers
Select New With {
.Id = c.CustomerID,
.Name = c.ContactName
}).ToList()
Dim dt As DataTable = ToDataTable(result)
Using wb As ClosedXML.Excel.XLWorkbook = New ClosedXML.Excel.XLWorkbook()
wb.Worksheets.Add(dt, "Customers")
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=Customers.xlsx")
Using MyMemoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
wb.SaveAs(MyMemoryStream)
MyMemoryStream.WriteTo(Response.OutputStream)
Response.Flush()
Response.[End]()
End Using
End Using
End Sub
Public Function ToDataTable(Of T)(ByVal items As List(Of T)) As DataTable
Dim dataTable As DataTable = New DataTable(GetType(T).Name)
Dim Props As PropertyInfo() = GetType(T).GetProperties(BindingFlags.Public Or BindingFlags.Instance)
For Each prop As PropertyInfo In Props
dataTable.Columns.Add(prop.Name)
Next
For Each item As T In items
Dim values = New Object(Props.Length - 1) {}
For i As Integer = 0 To Props.Length - 1
values(i) = Props(i).GetValue(item, Nothing)
Next
dataTable.Rows.Add(values)
Next
Return dataTable
End Function
Screenshot