Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi!

I was getting this error.

VB
Private Sub DATAGRIDVIEW_TO_EXCEL(ByVal DGV As DataGridView)
        Dim f As FolderBrowserDialog = New FolderBrowserDialog
        Try
            If f.ShowDialog() = DialogResult.OK Then 
                'This section help you if your language is not English.
                System.Threading.Thread.CurrentThread.CurrentCulture = _
                System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
                Dim oExcel As Excel.Application
                Dim oBook As Excel.Workbook
                Dim oSheet As Excel.Worksheet
                oExcel = CreateObject("Excel.Application")
                oBook = oExcel.Workbooks.Add(Type.Missing)
                oSheet = oBook.Worksheets(1)

                Dim dc As System.Data.DataColumn
                Dim dr As System.Data.DataRow
                Dim colIndex As Integer = 0
                Dim rowIndex As Integer = 0

                'Fill data to datatable
                Dim DTB As New DataTable <--- Error occurs here
                Dim RWS As Integer
                Dim CLS As Integer

                For CLS = 0 To DGV.ColumnCount - 1 ' COLUMNS OF DTB
                    DTB.Columns.Add(DGV.Columns(CLS).Name.ToString)
                Next

                Dim DRW As DataRow

                For RWS = 0 To DGV.Rows.Count - 1 ' FILL DTB WITH DATAGRIDVIEW
                    DRW = DTB.NewRow

                    For CLS = 0 To DGV.ColumnCount - 1
                        Try
                            DRW(DTB.Columns(CLS).ColumnName.ToString) = DGV.Rows(RWS).Cells(CLS).Value.ToString
                        Catch ex As Exception

                        End Try
                    Next

                    DTB.Rows.Add(DRW)
                Next


                'Export the Columns to excel file
                For Each dc In DTB.Columns
                    colIndex = colIndex + 1
                    oSheet.Cells(1, colIndex) = dc.ColumnName
                Next

                'Export the rows to excel file
                For Each dr In DTB.Rows
                    rowIndex = rowIndex + 1
                    colIndex = 0
                    For Each dc In DTB.Columns
                        colIndex = colIndex + 1
                        oSheet.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
                    Next
                Next

                'Set final path
                Dim fileName As String = "\ExportedAuthors" + ".xls"
                Dim finalPath = f.SelectedPath + fileName
                oSheet.Columns.AutoFit()
                'Save file in final path
                oBook.SaveAs(finalPath, XlFileFormat.xlWorkbookNormal, Type.Missing, _
                Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, _
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

                'Release the objects
                ReleaseObject(oSheet)
                oBook.Close(False, Type.Missing, Type.Missing)
                ReleaseObject(oBook)
                oExcel.Quit()
                ReleaseObject(oExcel)
                'Some time Office application does not quit after automation: 
                'so i am calling GC.Collect method.
                GC.Collect()

                MessageBox.Show("Export done successfully!")

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK)
        End Try

    End Sub


Can someone explain please? Thank you
Posted
Updated 7-Jul-14 15:38pm
v2
Comments
[no name] 7-Jul-14 21:20pm    
Dim f As FolderBrowserDialog = New FolderBrowserDialog() <-
mitchiee1226 7-Jul-14 21:21pm    
@Wes Aday added that one. :)
Dave Kreskowiak 7-Jul-14 21:27pm    
You could also just do it like this:

Dim f As New FolderBrowserDialog

VB.NET doesn't really care about the parenthesis in a New like C# does.
mitchiee1226 7-Jul-14 21:35pm    
@Dave Kreskowiak changed it to this but still has the error. If this helps here are the Imports I have added to the form:

Imports System.Data.SqlClient.SqlConnection
Imports System.Data.SqlClient
Imports System.IO.Directory
Imports System.Configuration
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop
Dave Kreskowiak 7-Jul-14 23:35pm    
You don't Import classes. You can remove the System.Data.SqlClient.SqlConnection and System.Io.Directory lines.

add using statement
C#
using System.Windows.Forms;
using System.Data

or use full class name
VB
Dim f As System.Windows.Forms.FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog

And
C#
Dim DTB As New System.Data.DataTable
 
Share this answer
 
v2
Use this instead:
Imports System.Data



    Dim dt As New System.Data.DataTable


The problem is probably the compiler confusing DataTable with some interface of the same name in the Excel namespace. If you didn't use Excel, you wouldn't have this problem.
 
Share this answer
 
'Fill data to datatable
                Dim DTB As New DataTable <--- Error occurs here

A Dim Statement (Visual Basic) uses a New (Visual Basic) clause when declaring a variable to be of an interface type.
Although an interface is a reference type, you cannot create an instance of it. You can use New only to create an instance of a class or a structure.


Normally in DataTable case it's conflicts happening between the namespace. better use full namespace and see if it again throws error.

Dim dt As New System.Data.DataTable
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-14 0:19am    
This would-be answer is full of gibberish.

"Although an interface is a reference type, you cannot create an instance of it." Following logic, you would say that you cannot create an instance of any class or array, because those types are references types. In fact, this is pure lie.

Next phrase: "You can use New only to create an instance of a class or a structure." Class is a reference type, and structure is a value type. So, where is logic?

Look, it's okay to make mistakes, to be unaware of something. But if you dare to answer questions of other people, you need to be extra responsible. Your mistake can spoil a big part of work of some beginners who cannot tell true from false. If you are not so confident, please, don't do evil: don't answer, better learn more. Please...

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900