-
Jul 24th, 2024, 04:47 PM
#1
Thread Starter
Junior Member
Export DGV to Word with column headers
Hello
How can I export the Datagridview with column headers to Word???
Does anyone have an example code for me?
Thanks in advance....
Last edited by jmcilhinney; Jul 25th, 2024 at 10:48 PM.
Reason: Translated to English
-
Jul 25th, 2024, 10:49 PM
#2
Re: Export DGV to Word with column headers
This is an English-only site. Please post only in English. I have translated your post and title to English this time. Any future threads posted in another language will be closed.
-
Jul 25th, 2024, 10:50 PM
#3
Re: Export DGV to Word with column headers
What have you done and where are you stuck?
-
Jul 26th, 2024, 08:00 AM
#4
Thread Starter
Junior Member
Re: Export DGV to Word with column headers
Hello, I got this code from an online forum.
I use it to export the DGV2 to Word, but the header texts are not included. According to the forum it should work but not for me.
Code:
Dim r As Integer, c As Integer
Dim oTable As Word.Table = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range,
Datagridview2.RowCount, Datagridview2.ColumnCount, Datagridview2.ColumnHeadersVisible = True)
For r = 0 To Datagridview2.RowCount - 2
For c = 0 To Datagridview2.Columns.Count - 1
oTable.Cell(r + 1, c + 1).Range.Text = Datagridview2(c, r).Value.ToString()
With objDoc.Tables(1)
.ApplyStyleHeadingRows = False
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
.ApplyStyleRowBands = False
.ApplyStyleColumnBands = False
.Columns(1).Width = 20
.Columns(2).Width = 46
.Columns(3).Width = 300
.Columns(4).Width = 70
.Columns(5).Width = 70
End With
-
Jul 26th, 2024, 09:47 AM
#5
Re: Export DGV to Word with column headers
So your question is effectively how to get the column header text from the grid. This is why it's important to provide a FULL and CLEAR explanation of the actual problem. Someo9ne could have wasted a lot of time telling you stuff that you already know. Each column has a HeaderText property. Seems useful, no? You need to loop through the columns, which you know how to do because you're already doing it, and get that property value from each one.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|