Export to Excel from Datagrid?

Hey everyone, this is my first post here, so I'll get right to the point.

I need to determine the best way to export data in a datagrid or dataset from a VB.NET Windows Form application directly into Excel.

I was able to find reasonable ways using Webforms, however have not found any except using the clipboard which is not practical in my case.  Also, the target version is Excel 2000, therefore I can't use XML.

This project involves converting an MS Access application to a VB.NET forms application and requires exporting to Excel regularly.  In Access I used the Transferspreadsheet command
as illustrated: http://msdn.microsoft.com/library/default.asp url=/library/en-us/off2000/html/acacttransferspreadsheet.asp

Thanks,
Lee


Answer this question

Export to Excel from Datagrid?

  • Ken Alexander

    how to export data from data grid to excell in window based application
  • LAUJ

    maybe you should just serialize the datasource to a file and import that in to excel.

    ds.writexml

  • Trond Brande

    This is altenative for export from data grid to excel....

    Try

    View_Data()

    Dim objExcel As New OWC.Spreadsheet

    Dim myRow As DataRow

    Dim myColumn As New DataColumn

    Dim RowCount As Object

    Dim ColumnCount As Object

    Dim FileAndPath As String = Trim(txtFileName.Text)

    ColumnCount = 0

    RowCount = 0

    For Each myColumn In objDataTable.Columns

    ColumnCount += 1

    RowCount = 1

    For Each myRow In objDataTable.Rows

    RowCount += 1

    objExcel.ActiveSheet.Cells(RowCount, ColumnCount) = CStr(myRow(myColumn))

    Next

    Next

    ColumnCount = 0

    For Each myColumn In objDataTable.Columns

    ColumnCount += 1

    objExcel.ActiveSheet.Columns(ColumnCount).EntireColumn.AutoFitColumns()

    Next

    objExcel.ActiveSheet.Export(FileAndPath, OWC.SheetExportActionEnum.ssExportActionNone)

    View_Data()

    Catch When Err.Number <> 0

    MsgBox("Tidak dapat membuat hubungan ke database" _

    & vbCrLf & Err.Description)

    Exit Sub

    End Try

    MsgBox("Export Success")

    You must have "Interopt.OWC.dll" for running this listing... you can look for dll from internet (use search engine).

    Jebat

    Thanks


  • Miles Cohen - MSFT

    Hello,

    I have tried it out this way , using adodc.recordset.movefirst and movenext options inside a For -loop

    Dim wapp As Excel.Application
    Dim wsheet As Worksheet
    Dim wbook As Workbook


    Set wapp = New Excel.Application
    Set wbook = wapp.Workbooks.Add()
    Set wsheet = wapp.Sheets(1)

    Adodc1.Recordset.MoveFirst
    ----Header -----You Number of Columns in Datagrid 
    For k = 0 To 5
    wsheet.Cells(1, k + 1).Value = DataGrid1.Columns(k).Caption
    Next

    ---Detailed Rows of Data Grid-------
    For i = 0 To Adodc1.Recordset.RecordCount - 1
    j = i + 2
    For k = 0 To 5
    wsheet.Cells(j, k + 1).Value = DataGrid1.Columns(k)
    Next
    Adodc1.Recordset.MoveNext
    Next

    wsheet.SaveAs (Your Name of the file)
    wapp.Workbooks.Close

    ----------------------------

  • Export to Excel from Datagrid?