vb.net datagridview导出到excel

时间:2017-02-06 02:37:21

标签: excel vb.net

嗨我有关于我在vb.net中的代码的问题,看来这段代码是正确的,但结果只是生成文件而不是我在这里创建的excel文件中的数据是我的代码

Dim xlApp As 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

    Try
        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add()
        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:\Users\CLB Engineering Mkt\Desktop\moveoutreport.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)
        MsgBox("Success")

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

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

而且我在gridview中的数据表就像这样......

如果不是con.State = ConnectionState.Open那么             con.Open()

    End If

    Dim da As New MySqlDataAdapter("select stock_number, item_description, quantity, unit_measure, customers_name, f_date from tbl_move_out", con)
    Dim dt As New DataTable

    da.Fill(dt)
    DataGridView1.DataSource = dt
    con.Close()
你可以帮我解决这个问题吗?提前谢谢你

0 个答案:

没有答案