Comment on Tutorial - How to export from DataGridView to excel using VB.net By Issac



Comment Added by : Alphin

Comment Added at : 2012-07-06 10:57:07

Comment on Tutorial : How to export from DataGridView to excel using VB.net By Issac
I am using VS 2010 Express and MS Excel 2007 wherein I am trying to export data from the DataGridView into an Excel file.

xlWorkSheet.SaveAs(“C:\ExportTagDetails.xlsx”)

the above line of code works for me. But if I again try to create a file, it obviously shows the Message : ‘A file named ‘C:\ExportTagDetails.xlsx’ already exists in this location. Do you want to replace it?’
When I press Yes it creates no problem and calmly replaces itself in place of the old file. But if I click on No or Cancel then it throws an exception stating : ‘System.Runtime.Interop.Services.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC’

Here is the entire block of code :
Private Sub cmd_export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_export.Click

Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
Dim row As Long = 1

xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets(“Sheet1″)
xlWorkSheet.Cells(1, 1).Font.Bold = True
xlWorkSheet.Cells(1, 1).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
xlWorkSheet.Cells(1, 2).Font.Bold = True
xlWorkSheet.Cells(1, 2).Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
xlWorkSheet.Columns(1).ColumnWidth = 45
xlWorkSheet.Columns(2).ColumnWidth = 30
xlWorkSheet.Range(“A” & row).Value = “Tag Name”
xlWorkSheet.Range(“B” & row).Value = “Start Value”
row = row + 1
Try
For i = 0 To DataGridView1.RowCount – 2
For j = 0 To DataGridView1.ColumnCount – 1
xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString()
Next
Next

xlWorkSheet.SaveAs(“C:\ExportTagDetails.xlsx”)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
xlWorkBook.Close()
xlApp.Quit()

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

MsgBox(“You can find the file at C:\ExportTagDetails.xlsx”)
End Sub

Is there any way I can handle the issue.
Or can you provide me with a solution where the user can himself enter the name of the file and also specify the location.
Thanks in advance.


View Tutorial



Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Whenever i run this program, it terminates immedia
View Tutorial          By: Salman at 2015-03-26 08:23:32

2. It is very useful ....Thank U sooo much dear...
View Tutorial          By: Augustine Francis at 2014-06-24 16:55:21

3. supb yar...very helpful
View Tutorial          By: narender lakshetty at 2010-07-23 23:20:00

4. Hi there, what a nice example, very useful for me
View Tutorial          By: Yanny at 2009-12-10 12:34:26

5. How to return values when there may be muliple row
View Tutorial          By: asd at 2013-11-22 01:24:47

6. Very good. Excellent examples and easy to understa
View Tutorial          By: Fernando Basso at 2012-09-30 22:17:56

7. Log onto incoming mail server(POP3) - failed
View Tutorial          By: dragos at 2012-12-13 10:41:59

8. Hi
I tried to use this class , I don't get

View Tutorial          By: arnav at 2015-03-27 06:01:22

9. hi,Very Nice tutorials and iwant to know how to ch
View Tutorial          By: vimala at 2010-07-08 05:05:17

10. I set my eclipse as describe above.
I also

View Tutorial          By: Anonymous at 2012-03-25 02:12:52