Hi experts,
I'am having a problem in exporting my data from Datagridview to my Excel it takes to much time at it gives me an error that said it have to much rows (i'am exporting a Rows of 5000).
How can i export my data using may datasource
Here is my code in Retrievingmy Data from Database to Datagridview
Here is how i'm exporting my data to excelCode:Dim con As New SqlConnection(sqlcon) Try con.Open() Dim cmd As SqlCommand = con.CreateCommand cmd.CommandText = "usp_GetReports" cmd.CommandType = CommandType.StoredProcedure Dim dr As SqlDataReader = cmd.ExecuteReader Dim dt As New DataTable dt.Load(dr) DataGridViewReportV1.DataSource = dt cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message & vbNewLine & "Error in Retrieving") Finally If con IsNot Nothing Then con.Close() con.Dispose() End If End Try
Thank you for your guidance.Code: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") // This is where i have an error if my rows are to large /// For i As Integer = 1 To DatagridReport.Columns.Count xlWorkSheet.Cells(1, i) = DatagridReport.Columns(i - 1).HeaderText Next For i As Integer = 0 To DatagridReport.Rows.Count - 2 For j As Integer = 0 To DatagridReport.Columns.Count - 1 xlWorkSheet.Cells(i + 2, j + 1) = DatagridReport.Rows(i).Cells(j).Value.ToString() Next Next Dim sd As New SaveFileDialog 'declare save file dialog sd.Filter = "Excel File (*.xlsx)|*.xlsx" sd.Title = "Save an Excel File" sd.RestoreDirectory = True If sd.ShowDialog = Windows.Forms.DialogResult.OK Then 'check if save file dialog was close after selecting a path 'MsgBox(sd.FileName) xlWorkSheet.SaveAs(sd.FileName) 'sd.filename reurns save file dialog path MsgBox("You can find the file " + sd.FileName) xlWorkBook.Close() xlApp.Quit() End If releaseObject(xlApp) releaseObject(xlWorkBook) releaseObject(xlWorkSheet) 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




Reply With Quote