Hi thereallover0...,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using ClosedXML.Excel;
using System.IO;
VB.Net
Imports System.Data
Imports ClosedXML.Excel
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country", typeof(string))
});
dt.Rows.Add(1, "Mudassar Khan", "United States");
dt.Rows.Add(2, "John Hammod", "India");
dt.Rows.Add(3, "Robert Schidner", "France");
dt.Rows.Add(4, "Suzanne Mathews", "Russia");
using (XLWorkbook wb = new XLWorkbook())
{
var ws = wb.Worksheets.Add("worksheet");
// Adding HeaderRow.
ws.Cell("A1").Value = "Excelasoft Solutions";
ws.Cell("A1").Style.Font.Bold = true;
ws.Cell("A2").Value = "G-110, Shagun Arcade,Near Oberoi Mall,Malad (East),Mumbai 400097";
if (dt.Rows.Count > 0)
{
ws.Cell("A4").Value = dt.Columns[0].ColumnName;
ws.Cell("B4").Value = dt.Columns[1].ColumnName;
ws.Cell("C4").Value = dt.Columns[2].ColumnName;
// Adding DataRows.
for (int i = 0; i < dt.Rows.Count; i++)
{
ws.Cell("A" + (i + 5)).Value = dt.Rows[i][0];
ws.Cell("B" + (i + 5)).Value = dt.Rows[i][1];
ws.Cell("C" + (i + 5)).Value = dt.Rows[i][2];
}
}
ws.Column(2).AdjustToContents();
ws.Column(3).AdjustToContents();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Customer.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "Mudassar Khan", "United States")
dt.Rows.Add(2, "John Hammod", "India")
dt.Rows.Add(3, "Robert Schidner", "France")
dt.Rows.Add(4, "Suzanne Mathews", "Russia")
Using wb As XLWorkbook = New XLWorkbook()
Dim ws = wb.Worksheets.Add("worksheet")
ws.Cell("A1").Value = "Excelasoft Solutions"
ws.Cell("A1").Style.Font.Bold = True
ws.Cell("A2").Value = "G-110, Shagun Arcade,Near Oberoi Mall,Malad (East),Mumbai 400097"
If dt.Rows.Count > 0 Then
ws.Cell("A4").Value = dt.Columns(0).ColumnName
ws.Cell("B4").Value = dt.Columns(1).ColumnName
ws.Cell("C4").Value = dt.Columns(2).ColumnName
For i As Integer = 0 To dt.Rows.Count - 1
ws.Cell("A" & (i + 5)).Value = dt.Rows(i)(0)
ws.Cell("B" & (i + 5)).Value = dt.Rows(i)(1)
ws.Cell("C" & (i + 5)).Value = dt.Rows(i)(2)
Next
End If
ws.Column(2).AdjustToContents()
ws.Column(3).AdjustToContents()
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment;filename=Customer.xlsx")
Using MyMemoryStream As MemoryStream = New MemoryStream()
wb.SaveAs(MyMemoryStream)
MyMemoryStream.WriteTo(Response.OutputStream)
Response.Flush()
Response.End()
End Using
End Using
End Sub
Screenshot