Hello all;
I am trying to generate an excel file from a DataGridView, but I am getting an error (HRESULT : 0x800A03EC) when I do something like
vb.net Code:
xlSheet.Cells(y + 2, x + 1) = DataGridView1.Item(y, x)
As it seems the errors is because the column type is a button (Delete button)
I Think the solution is to check before making the mentionned line of code the type of the item(x,y), so how to do this please ?
Here is the full code that I am using, based on an article i FOUND on the net, if you have a better way to generate excel from DataGrid please let me know
vb.net Code:
Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim ds As DataSet ds = New DataSet ds = oData Try xlApp = CType(CreateObject("Excel.Application"), Excel.Application) xlBook = CType(xlApp.Workbooks.Add, Excel.Workbook) xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet) ' ici on compte le nombre de lignes et de colonnes du datatable Dim nbrLigne As Integer = ds.Tables("DECLARATION").Rows.Count - 1 Dim nbrColon As Integer = ds.Tables("DECLARATION").Columns.Count - 1 Dim x, y As Integer For x = 0 To nbrColon ' ici on prends le titre des colonnes du datatable xlSheet.Cells(1, x + 1) = ds.Tables("DECLARATION").Columns(x).ColumnName ' on mets la première ligne en gras xlSheet.Rows(1).Font.Bold = True ' pour chaque colonne et chaque ligne on transfert les données For y = 0 To nbrLigne xlSheet.Cells(y + 2, x + 1) = DataGridView1.Item(y, x) Next Next ' ici on affiche les résultat dans excel xlSheet.Application.Visible = True ' on peut sauvegarder notre document sur le disque xlSheet.SaveAs("c:\Documents and Settings\developpeur1\Bureau\MonExcel.xlsx") ' on quitte l'application et on détruit les objets xlApp.Quit() xlSheet = Nothing xlBook = Nothing xlApp = Nothing Catch ex As Exception MsgBox(ex.Message) End Try End Sub


Reply With Quote