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
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
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
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
Archived Comments
1. thank you very much....
View Tutorial By: divya at 2013-01-06 15:50:43
2. thank for this script
because i will work
View Tutorial By: r at 2009-06-27 05:07:31
3. Amazing tutorial!! This is what I was exactly look
View Tutorial By: Sumanth Vaidya at 2015-06-17 18:19:16
4. super
View Tutorial By: Dhana at 2007-01-30 03:21:00
5. I've tried this code but i am gettting error at li
View Tutorial By: Iqra at 2012-12-13 10:43:12
6. Good one
View Tutorial By: Soumya Anoop at 2010-05-13 23:23:44
7. it is very nice example of overloading when i read
View Tutorial By: narendra saini at 2012-04-20 04:03:56
8. Hey there, I was needing something exactly like th
View Tutorial By: Jeffry at 2013-03-26 03:33:27
9. Very good.
But is this (Accessor functions
View Tutorial By: peter at 2011-07-20 13:13:44
10. add reference -> .net -> 'Microsoft.Office.
View Tutorial By: Ravi at 2011-08-27 12:17:36