datagridview export to word very slow. faster way?

ridhwaans

Active member
Joined
Jun 1, 2012
Messages
34
Programming Experience
3-5
In my vb application, the user can export a datagridview to a table in MS word via a button click

I'm using Interop word with a MS Word 14.0 library reference to achieve this

The size of the datagridview can vary; but at most it could be as large as having 20 columns and 2000 rows

but with the code I am using, the exporting method seems to be awfully slow

VB.NET:
Dim r, c As Integer
        Dim datatabl As New System.Data.DataTable
        datatabl = DataGridView1.DataSource
        Dim oWord = New Word.Application
        'oWord.Visible = True '
        Dim oDocs As Word.Document
        oDocs = oWord.Documents.Add
        Dim otable As Word.Table

        otable = oDocs.Tables.Add(oDocs.Bookmarks.Item("\endofdoc").Range, datatabl.Rows.Count + 1, datatabl.Columns.Count)
        otable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
        otable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
        otable.Rows.Item(1).Range.Font.Bold = True

        For c = 0 To datatabl.Columns.Count - 1
            otable.Cell(1, c + 1).Range.Text = datatabl.Columns(c).ToString
        Next

        For r = 0 To datatabl.Rows.Count - 1
            For c = 0 To datatabl.Columns.Count - 1
                otable.Cell(r + 2, c + 1).Range.Text = datatabl.Rows(r).Item(c).ToString
            Next
        Next

        Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Filter = "Word Document|*.docx"
        saveFileDialog1.Title = "Export to Word"
        saveFileDialog1.FileName = fileSavePath("docx")
        saveFileDialog1.ShowDialog()

        If saveFileDialog1.FileName <> "" Then
            oDocs.SaveAs(saveFileDialog1.FileName)
            oDocs.Close(SaveChanges:=True) '
            oDocs = Nothing
            oWord.Quit() '
            oWord = Nothing

            GC.Collect()
            GC.WaitForFullGCComplete()

the export would take 7-10 seconds for datagridview with 500 rows, and longer there were 500+, 1000+ rows

could anyone suggest a much FASTER way to complete exporting to a table in ms word?

thanks
 
@VBobCat
exporting a dgv to an HTML table using streamwriter and the stringbuilder method is really very nice, but I would like to see a fast way to export dgv to word

@Dunfiddlin
exporting dgv to html is fast (takes ~1-2 seconds), regardless of the table size
did you a straight paste of the html table upon a word document?
 
Just as a point of comparison, how long would a straight copy and paste of a table this size take?

Thanks, I went researching and found out this, which is new to me. One can actually export data directly from DataGridView to the clipboard.
ridhwaans could then make this:

DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = True
DataGridView1.SelectAll()
Clipboard.SetDataObject(DataGridView1.GetClipboardContent)


Obs. The first and second lines might be unnecessary, they just make sure that the entire content of datagridview would be selected.
 
@VBobCat
exporting a dgv to an HTML table using streamwriter and the stringbuilder method is really very nice, but I would like to see a fast way to export dgv to word

@Dunfiddlin
exporting dgv to html is fast (takes ~1-2 seconds), regardless of the table size
did you a straight paste of the html table upon a word document?

Yes, when I posted my first reply, I was unaware of the possibility of copying the content of a DataGridView directly to Clipboard, as mentioned by Dunfiddlin. I looked up which methods one should use, and tested, with the code I posted in my second reply, and Clipboard.SetDataObject(DataGridView1.GetClipboardContent) is its core command. It worked fine. I tested it in a pretty large datagridview, so I think this is the very way you should proceed.
 
Back
Top