vb2008 export DGV to word ~ excel
(see also: export listview to word table: http://www.vbforums.com/showthread.php?t=624967)
vb2008 export DGV to word ~ excel
(see also: export listview to word table: http://www.vbforums.com/showthread.php?t=624967)
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Hi Paul, how can we append to an already existing excel file?
I have this code so far:
Am I missing anything or do I need to do something else? Would this work?Code:If Not File.Exists(Application.StartupPath & "\" & "test" & ".xlsx") Then <insert .Paul.'s original DVG to excel code here> 'I also set visibility = false because I want my program to auto-save without user interaction 'plus I added xlWorkBook.SaveAs(Application.StartupPath & "\" & "test" & ".xlsx") 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing DataGridView1.Rows.Clear() 'clear our datagridview table else <insert .Paul.'s original DVG to excel code with these following changes:> Dim xlApp as excel.application = new excel.Application Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "test" & ".xlsx") Dim xlWorkSheet As excel.Worksheet 'how do I select the current sheet on this file? 'And this line I modified the range to: Dim range As excel.Range = xlWorkSheet.UsedRange 'And then I have this at the end to save my excel file xlWorkBook.Save() 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing DataGridView1.Rows.Clear() 'clear our datagridview table when user clicks save
Last edited by zero_coke; Mar 9th, 2011 at 05:18 PM.
what do you want to append to your existing worksheet?
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Whenever my users click "save" I clear the datagridview contents. Then:
Whenever the user's click "Save" again, program should check if there is an existing excel file named "test" and append to the end of it the current DGV contents. Then the DGV is cleared again and basically this is repeated everytime the user clicks "Save".
However, if no such file named "test" exists then it should just make a new excel file and paste the DGV contents into it.
That's all. So if certain file exists then append to it (excluding the column header names) and if doesn't exist then paste all of the DGV contents to a new file.
Does that make any sense? Do you have any other questions? I hope this made it clear...
Last edited by zero_coke; Mar 9th, 2011 at 06:46 PM.
well you'd use similar code to my example, but you'd open the existing workbook, set xlWorkSheet to "sheet1" (same worksheet as originally used), copy your dgv + set the range allowing for the previously used cells, then paste the new information from your dgv.
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Yeah I think the code I pasted above does that when I made the appropriate changes but it doesn't work for some reason....I don't know what I'm missing...
ok post your complete code + i'll tell you where you're going wrong
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Can you also show me how to not write the headers when appending?Code:Private Sub exportToExcel(ByVal dgv As DataGridView) If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook Dim xlWorkSheet As excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) 'xlApp.Visible = True Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString) range.Select() xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing TestDataGridView.Rows.Clear() 'clear the log ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then MsgBox("bump, it exists") Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx") Dim xlWorkSheet As excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) 'xlApp.Visible = True Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.UsedRange range.Select() '? should have this or not? is it over-writing? xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With xlWorkBook.Save() 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing TestDataGridView.Rows.Clear() 'clear the log Else Debug.Print("What the hell? Either exists or not lol") End If End Sub
Last edited by zero_coke; Mar 9th, 2011 at 07:05 PM.
try this:
vb Code:
Private Sub exportToExcel(ByVal dgv As DataGridView) If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook Dim xlWorkSheet As excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) 'xlApp.Visible = True Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString) range.Select() xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing TestDataGridView.Rows.Clear() 'clear the email log ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then MsgBox("bump, it exists") Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx") Dim xlWorkSheet As excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) 'xlApp.Visible = True Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.Range("B" & (xlWorkSheet.UsedRange.rows.count + 1).tostring & ":" & alphabet(headerText.Length) & (xlWorkSheet.UsedRange.rows.count + items.Length).ToString) range.Select() '? should have this or not? is it over-writing? xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With xlWorkBook.Save() 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing TestDataGridView.Rows.Clear() 'clear the email log Else Debug.Print("What the hell? Either exists or not lol") End If End Sub
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
I'll try this and let you know.
In the mean time, do you know how to make the columns in excel file auto-size so I don't have to manually shift the cells width since they're too small and text overwrites onto next cell if you know what I mean.
add this after the other range formatting code:
vb Code:
range.Columns.AutoFit()
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Hey Paul, can you please upload the class file? I just copied and pasted your code and went through removing all the line numbers manually and still I'm getting a whole bunch of errors (the squiggly blue line under variables).
Also, can you please make that autofit adjustment? I'm not too sure where you mean by "other range" code.
i didn't make a class file. those squiggly lines probably mean you don't have the right imports in your class. which version of windows are you using? i can copy that code without the line numbers...
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Alright, that fixed it. First I removed the line numbers manually and then I made a program to remove the line numbers using regex I guess that messed it up.
So where exactly do I put the range.Columns.AutoFit() ?
EDIT: Nevermind, just saw it. I was checking the top the whole time meanwhile it was at the bottom.
Thanks Paul! I'll try this out, hopefully works without flaws. I'll keep you updated.
Uh oh...it is not appending...What do I do now Paul?
ok. can you upload your project + i'll debug it.
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
I'm sorry Paul, I can't upload the project. It's just some agreement that I have with my client and partners. However, I greatly appreciate your help. Would you like me to make you a test project and see if it appends?
actually i think i found it. in the append part remove this line:
vb Code:
xlWorkBook = xlApp.Workbooks.Add(misValue)
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
When I do that it I can't open the excel file at all. Just opens and closes really fast.
ok. post the code again
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
vb Code:
Private Sub exportToExcel_new(ByVal dgv As DataGridView) If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook Dim xlWorkSheet As excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString) range.Select() xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With range.Columns.AutoFit() 'auto-size the columns so the columns look nice xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing DataGridView1.Rows.Clear() 'clear the log ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then MsgBox("bump, it exists") Dim xlApp As excel.Application = New excel.Application Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx") Dim xlWorkSheet As excel.Worksheet 'Dim misValue As Object = System.Reflection.Missing.Value 'xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet) Dim headers = (From ch In dgv.Columns _ Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _ Select header.Value).ToArray() Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString) Dim items() = (From r In dgv.Rows _ Let row = DirectCast(r, DataGridViewRow) _ Where Not row.IsNewRow _ Select (From cell In row.Cells _ Let c = DirectCast(cell, DataGridViewCell) _ Select c.Value).ToArray()).ToArray() Dim table As String = String.Empty For Each a In items Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString) table &= String.Join(vbTab, t) & Environment.NewLine Next table = table.TrimEnd(CChar(Environment.NewLine)) Clipboard.SetText(table) Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray Dim range As excel.Range = xlWorkSheet.Range("B" & (xlWorkSheet.UsedRange.Rows.Count + 1).ToString & ":" & alphabet(headerText.Length) & (xlWorkSheet.UsedRange.Rows.Count + items.Length).ToString) range.Select() '? should have this or not? is it over-writing? xlWorkSheet.Paste() range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone With range.Borders(excel.XlBordersIndex.xlEdgeLeft) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeTop) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeBottom) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlEdgeRight) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlMedium End With With range.Borders(excel.XlBordersIndex.xlInsideVertical) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With With range.Borders(excel.XlBordersIndex.xlInsideHorizontal) .LineStyle = excel.XlLineStyle.xlContinuous .ColorIndex = 1 'black .TintAndShade = 0 .Weight = excel.XlBorderWeight.xlThin End With range.Columns.AutoFit() 'auto-size the columns so the excel columns look nice xlWorkBook.Save() 'save our workbook 'releasing object references xlWorkBook = Nothing xlWorkBook = Nothing xlApp.Quit() xlApp = Nothing DataGridView1.Rows.Clear() 'clear the log Else Debug.Print("What the hell? Either exists or not lol") End If End Sub
it does work. you don't see it because you removed:
vb Code:
xlApp.Visible = True
+ you close excel after the code has run, which is almost instantly
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Yeah but it should at least be saved right? I mean, i have
vb Code:
xlWorkBook.Save() 'save our workbook
At the end so would it matter if the visibility was on or off? And I don't want the visibility to be on, I just want the program to save it automatically without showing the user. Do I have to turn visibility on?
have you tried opening the file in excel? it works + saves whether you make excel visible or not
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Here's my tester project. It doesn't save for some reason. Can you have a look at it please Paul?
Thanks
It's 4 am in the morning here. I'll be back tomorrow Paul. Thanks!
Last edited by zero_coke; Mar 10th, 2011 at 01:05 AM.
you didn't remove these lines from the append part as i told you to:
vb Code:
Dim misValue As Object = System.Reflection.Missing.Value xlWorkBook = xlApp.Workbooks.Add(misValue)
if you remove these lines it works
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
also, before the 2nd range.columns.autofit, put this:
vb Code:
range = xlWorkSheet.Range("B2:" & alphabet(xlWorkSheet.UsedRange.Columns.Count) & (xlWorkSheet.UsedRange.Rows.Count).ToString)
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Yay! Thanks Paul it works now! It appends exactly the way I wanted to!
So Paul, final question if you do not mind:
How can you get the application to overwrite the existing file automatically? When I click save I see this:
When you click "no" my debugger points out an error on some line because I think the user must click Yes or the append part of the code results in an error.
I don't even know why Excel is prompting to replace the file when you're just opening it, writing some data do it and saving it.
i'm not sure about that. that didn't happen when i ran it.
start a new thread for that...
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Hey Paul,
Excel doesn't prompt me anymore. I made a stupid mistake and added
vb Code:
xlapp.SaveWorkspace()
So I removed that ^ and it works now.
Just one question: When a cell is empty, the code gives me an error here:
ERROR: Object reference not set to an instance of an object.
Error location:
For Each a In items
Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
table &= String.Join(vbTab, t) & Environment.NewLine
Next
And it highlights the Function(v) v.ToString) part.
You can check if a cell is empty by:
But I don't know where to put it. Seems like you're using an array function...Code:String.IsNullOrEmpty(CStr(DataGridView1.Item(i, x).Value))
this works:
Dim t() As String = Array.ConvertAll(a, Function(v) if(not v is nothing, v.ToString, ""))
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Awesome, that works
I want to ask one more question. May I? I really don't want to bother you anymore. You've done so much.
what's the question?
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
The excel application doesn't close. When I go to task manager, its still there so how can I close it properly?
I could use
But I don't think this is the right way to end it right?Code:Dim proc As System.Diagnostics.Process For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") proc.Kill() Next
Last edited by zero_coke; Mar 10th, 2011 at 11:26 AM.
try:
xlworkbook.close
xlapp.close (or dispose)
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Hi Paul,
I'm having trouble making the gridview go to a specific bookmark, it's probably my ignorance. How would I go about doing this?
Thanks,
Verra
my example uses a new word doc with no bookmarks set. i'm assuming you're using an existing word doc with predefined bookmarks?
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
Yes, I've got a few data grid views which will display in a report in a word document; all the bookmarks are pre-defined.
could you post the word doc you're using + clarify which dgv should go where?
![]()
![]()
if this helps, rate me
![]()
![]()
irregular regions | numericTextbox | keycodes | removing control properties | hotkeys | rtf printing | Extended RichTextBox | iconWorks | readonly listbox | sort listview | screen capture | relational listForms | countDown timer | animated notifyIcon form | dynamic crystal report | move / resize runtime controls | reOrderable DnD listview / listbox | self closing message box | searchable list(of class) | cursor from bitmap | (VB2008+) Textbox - GetFirstVisibleLineIndex / GetLastVisibleLineIndex | vb2008 extensions | Paint lite.Net | calculator | inline calculator | export listview to word table | imperial~metric converter | export DGV to word ~ excel | globalInputHook | dropDown Calendar control | International Time + Currency | GDI+ gauge | quizControl | extended dgv
My Web Site....
Maths Revision V1.0
Play Connect4 against your PC (java Applet)
The word document I'm using is HUGE, approx 200 pages. Here's an example of part of it:
"Blah blah blah assets etc (talks about assets)
[Bookmark here for Assets Summary]
more text here about other things
-a few pages later-
text about liabilities
[Bookmark for Liabilities Summary]
then more text"
The bookmarked grid views don't get anymore complex than that, just sticking them after or before paragraphs and headings.