Click here to Skip to main content
11,707,470 members (75,778 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi
How to export DataGridview to Excel with Headers?
How to export cells selected(DataGridview) to Excel?
Posted 31-Jan-13 9:56am
Edited 31-Jan-13 9:57am
v2
Comments
Sergey Alexandrovich Kryukov at 31-Jan-13 15:59pm
   
What did you try so far? Where is the problem: working with DataGridView? working with Excel? what?
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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()
  Permalink  
v2
Comments
sudevsu at 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 at 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 at 29-May-15 12:11pm
   
Oh I really didn't see that. I saw that it is sitting in questions and I answered.
CHill60 at 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 at 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)

  Print Answers RSS
0 Mika Wendelius 220
1 Maciej Los 195
2 OriginalGriff 148
3 Sergey Alexandrovich Kryukov 145
4 chainerlt 110
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 28 May 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100