Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Frnds,

I need to export the collapse gridview to excel. i have the problem in this code. Type or namespace 'XLWorkbook' could not be found. what is the problem here.

DataTable dt = new DataTable("GridView_Data");
GridView gvOrders = (GridView)gvCustomers.Rows[1].FindControl("gvOrders");
foreach (TableCell cell in gvCustomers.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
foreach (TableCell cell in gvOrders.HeaderRow.Cells)
{
dt.Columns.Add(cell.Text);
}
dt.Columns.RemoveAt(0);
foreach (GridViewRow row in gvCustomers.Rows)
{
GridView gvOrderscell = (row.FindControl("gvOrders") as GridView);
for (int j = 0; j < gvOrderscell.Rows.Count; j++)
{
dt.Rows.Add(row.Cells[1].Text, row.Cells[2].Text, gvOrderscell.Rows[j].Cells[0].Text, gvOrderscell.Rows[j].Cells[1].Text);
}
}
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);

Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
Posted
Updated 18-Jan-22 3:43am
v2

Have you referenced the required dlls?
Do the below.

On your Solution Explorer > Reference > Right click > Add Reference > COM Tab > Search for Microsoft Excel 14.0 Object Library (or lower depending on your excel.)
 
Share this answer
 
Comments
Vivek.anand34 21-Oct-15 9:11am    
On my Solution Explorer > Reference > Right click > Add Reference > Project Tab
I add 2 references: 1. ClosedXML dll and
2. DocumentFormat.OpenXML dll

Now Works Well.. Thank You for you reference.
It worked for me. Use the solution provided.
 
Share this answer
 
Comments
Richard Deeming 18-Jan-22 12:24pm    
If you want to reply to the existing solution, click the "Have a Question or Comment?" button under that solution and post a comment. Do not post your comment as a solution to the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900