Tag Archives: DataGridView Control

Export the content of DataGridView to Word Document in vb.net

Several days past i got a comment of my user on the article ‘Create a new Word document using VB.Net‘. He was facing the trouble to export the image to Word document. He wrote his problem as:

I have a Datagridview and all those three fields has been showed in the Datagridview , I have a button Called ” Export to Word”.

There are three columns in the DataGridView, one of them is image type and now we need to export all the content of this Datagridview to the table in the word document. My example contains a DataGridView control named ‘DataGridView1’ that have three columns. Two columns are the TextBox Columns and rest one is of the Image type.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindGrid()
    End Sub
 
    Private Sub BindGrid()
        DataGridView1.Rows(0).Cells(0).Value = 1
        DataGridView1.Rows(0).Cells(1).Value = "Mohan"
 
        Dim _cellimage As New DataGridViewImageCell
        Try
            DataGridView1.Rows(0).Cells(2).Value = Image.FromFile("D:\Closed.png")
        Catch ex As Exception
 
        End Try
 
    End Sub

There is another button control ‘Export to Document’. On the Click of this button, we will export the content of the datagridview to word document.

    Private Sub btnExport_Click(ByVal sender As System.Object, _
                     ByVal e As System.EventArgs) Handles btnExport.Click
        CreateWordDocument()
    End Sub

The below function will create a word document and export the datagridview.

    Private Sub CreateWordDocument()
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
 
        objWord = CreateObject("Word.Application")
        objWord.Visible = True
        objDoc = objWord.Documents.Add
 
        Dim _RowCount As Integer = DataGridView1.Rows.Count - 1
        Dim _ColCount As Integer = DataGridView1.Columns.Count - 1
 
        Dim ht1 As Word.Table
 
        ht1 = objDoc.Tables.Add(objDoc.Bookmarks.Item("\endofdoc").Range, _
                                _RowCount + 1, _ColCount + 1)
        ht1.Borders.OutsideColor = Word.WdColor.wdColorBlack
        ht1.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
        ht1.Borders.InsideColor = Word.WdColor.wdColorBlack
        ht1.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
 
        For i As Integer = 0 To _RowCount
            ht1.Rows.Add()
            For _col As Integer = 0 To _ColCount
                Dim colType As Type = DataGridView1.Columns(_col).GetType
                If colType.Name = "DataGridViewImageColumn" Then
                    Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).Value, Image)
                    Clipboard.SetImage(_image)
                    ht1.Cell(i + 1, _col + 1).Range.Paste()
                Else
                    ht1.Cell(i + 1, _col + 1).Range.Text = _
                    DataGridView1.Rows(i).Cells(_col).Value.ToString()
                End If
            Next
        Next
        objDoc.SaveAs2("C:/tee.docx")
    End Sub

If you get error in the Following line:
Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).Value, Image)

Try to replace this line with the following (Suggested by: Xabier Aberasturi)
(Put “FormattedValue” instead of “Value”)
Dim _image As Image = DirectCast(DataGridView1.Rows(i).Cells(_col).FormattedValue, Image)