hi am having problem in saving datagridview data to excel file.. i have import excel namespace into my project but it shows

error : Namespace or type specified in the Imports 'class2' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.


please help me ..

Imports Excel = Microsoft.Office.Interop.Excel

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")

        For i = 0 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                    DataGridView1(j, i).Value.ToString()
            Next
        Next

        xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()

        MsgBox("You can find the file C:\vbexcel.xlsx")

Edited 5 Years Ago by prishe: n/a

Hope the code is in some function. Because the VS pointed to adding "public" directive of this function.
I want to let you know that the MS Automation works very slow, and required excel installed on the server. As an alternative you can use some free 3rd party's component.
For example, this <<snip>>

Edited 5 Years Ago by Nick Evan: removed plug

Have you actually got the interop for excel. I did this a while ago for word and access and had to download and install the interop assemblies from MS website to the GAC (Global Assembly Cache).

Oops, moderator improved my msg. Who are still interesting in suggestion write me private message!

This article has been dead for over six months. Start a new discussion instead.