none
How to convert the datagridview into excel file ?

    Question

  • Please inform what my mistake is.

    By write down the program source, below

    Imports Excel = Microsoft.Office.Interop.Excel
            Dim APP As New Excel.Application
            Dim worksheet As Excel.Worksheet
            Dim workbook As Excel.Workbook
            workbook = APP.Workbooks.Open(excelLocation)
            worksheet = workbook.Worksheets("sheet1")
            Dim columnsCount As Integer = DataGridView1.Columns.Count
            For Each column In DataGridView1.Columns
                worksheet.Cells(1, column.Index + 1).Value = column.Name
            Next
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim columnIndex As Integer = 0
                Do Until columnIndex = columnsCount
                    worksheet.Cells(i + 2, columnIndex + 1).Value = DataGridView1.Item(columnIndex, i).Value.ToString
                    columnIndex += 1
                Loop
            Next

    The character, that I bold in black color is written down with red colored line in VB.net

    It means that this command has not known by VB.net

    Please give me a corrective code.

    Thursday, April 27, 2017 1:59 PM

All replies

  • Have you checked you have a reference to Excel for the Imports to work?

    It needs to be in the project References, add with Project/Add reference

    If that doesn't resolve your "red squiggles", then post some more of your code here...


    Thursday, April 27, 2017 2:19 PM
  • Hi MulJanto,

    Please check if you have add Microsoft Excel Object Library and Microsoft.Office.Interop.Excel dll in the Reference, then please refer to the code below.

    and then right-click Add Reference---click Browse---choose field

    Imports Excel = Microsoft.Office.Interop.Excel Public Class Excel Private Sub Excel_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'Database1DataSet.Table1' table. You can move, or remove it, as needed. Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim xlApp As Microsoft.Office.Interop.Excel.Application Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet Dim misValue As Object = System.Reflection.Missing.Value Dim i As Integer Dim j As Integer xlApp = New Microsoft.Office.Interop.Excel.Application xlWorkBook = xlApp.Workbooks.Add(misValue) xlWorkSheet = xlWorkBook.Sheets("sheet1") For i = 0 To DataGridView1.RowCount - 2 For j = 0 To DataGridView1.ColumnCount - 1 For k As Integer = 1 To DataGridView1.Columns.Count xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() Next Next Next xlWorkSheet.SaveAs("D:\vbexcel.xlsx") xlWorkBook.Close() xlApp.Quit() releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) MsgBox("You can find the file D:\vbexcel.xlsx") End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 28, 2017 5:20 AM
    Moderator
  • Hi Muljanto,

    Please remember to close your thread by marking helpful post as  answer, it is very beneficial to the other communities who face the same issue.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 2, 2017 1:23 AM
    Moderator