What is wrong in these codes, it do nothing
Please help

Dim oExcel As Object
        Dim oBook As Object
        Dim osheet As Object
        oExcel = CreateObject("Excel.Application")
        oBook = oExcel.Workbooks.Add
        osheet = oBook.sheets("sheet1")

        'Paste the data.
        Dim j As Integer
        For i = 0 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                oBook.Cells(i + 1, j + 1) = DataGridView1(j, i).Value.ToString()
            Next
        Next

        'Save the workbook and quit Excel.
        oBook.SaveAs("c:\Book5.xls")
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
        GC.Collect()

Recommended Answers

All 2 Replies

I think you should have ActiveSheet in the assignment line (12)

ExcelSheetFO.ActiveSheet.Cells(2, i) = "Some value"

I think you should have ActiveSheet in the assignment line (12)


I added this line

ExcelSheetFO.ActiveSheet.Cells(2, i) = "Some value"

But it still not work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.