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
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
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