Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hi How to export DataGridview to Excel with Headers?
How to export cells selected(DataGridview) to Excel?
Posted
Updated 31-Jan-13 9:57am
v2
Comments
Sergey Alexandrovich Kryukov 31-Jan-13 15:59pm    
What did you try so far? Where is the problem: working with DataGridView? working with Excel? what?
—SA

I am using this code for export datagridview to Excel:
Export DataGridView to Excel in vb.net 2.0 Windows Form[^]

But How to export DataGridview's Headers to Excel? (Picture)

Also How to export cells selected(DataGridview) to Excel? (Picture)
 
Share this answer
 
v2
I use EE plus. which was good option suggested by
Sergey Alexandrovich Kryukov
Thanks to SA once again.
Here is my code... This code doesn't take to selected rows to Excel. But it stores entire Gridview. You can just change the Dataset with selected Rows of Gridview. That works
Dim file As FileInfo = New FileInfo("ClaimForms.xls")
       Using pck As New ExcelPackage(file)
           Dim wsClaims As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet1")
           wsClaims.TabColor = Color.Blue

           Dim wslineitems As ExcelWorksheet = pck.Workbook.Worksheets.Add("Sheet2")
           wslineitems.TabColor = Color.Blue

           Dim dsExcel As DataSet = dbnet.GetAllDetails()
           Dim dtClaims As DataTable = dsExcel.Tables(0)
           Dim dtLineItems As DataTable = dsExcel.Tables(1)

           wsClaims.Cells("A1").LoadFromDataTable(dtClaims, True)
           wslineitems.Cells("A1").LoadFromDataTable(dtLineItems, True)
           wsClaims.Cells.AutoFitColumns()
           wslineitems.Cells.AutoFitColumns()
           Using rng As ExcelRange = wsClaims.Cells(1, 1, 1, dtClaims.Columns.Count)
               rng.Style.Font.Bold = True
               rng.Style.Fill.PatternType = ExcelFillStyle.Solid
               rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Blue)
               rng.Style.Font.Color.SetColor(System.Drawing.Color.Yellow)
           End Using
           Using rng As ExcelRange = wslineitems.Cells(1, 1, 1, dtLineItems.Columns.Count)
               rng.Style.Font.Bold = True
               rng.Style.Fill.PatternType = ExcelFillStyle.Solid
               rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Blue)
               rng.Style.Font.Color.SetColor(System.Drawing.Color.Yellow)
           End Using
           Dim result As MemoryStream = New MemoryStream
           pck.SaveAs(result)
           Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
           Dim fileName As String = String.Format("ClaimReport_{0:yyyyMMdd_HH_mm}.xlsx", DateTime.Now)
           Dim disposition As String = String.Format("attachment; filename=""{0}""", fileName)
           Response.AppendHeader("Content-Disposition", disposition)
           result.WriteTo(Response.OutputStream)
                      Response.End()
 
Share this answer
 
v2
Comments
sudevsu 28-May-15 15:13pm    
who ever downvoted this? Can you please tell me why? Nothing offensive, I just want to know whether it is wrong with code or the approach?
CHill60 28-May-15 19:36pm    
Probably because it's a response to a question that was asked well over 2 years ago and already had an answer. Personally I think you may have added something to the subject, but normally it doesn't go down well. It's usually best to avoid posting solutions to old posts
sudevsu 29-May-15 12:11pm    
Oh I really didn't see that. I saw that it is sitting in questions and I answered.
CHill60 29-May-15 12:17pm    
We've all been caught out like that :) Sometimes spammers hit old posts then when their "solution" (spam) is deleted the question pops back to the top of the queue
sudevsu 1-Jun-15 15:31pm    
Oh Really? Crazy

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