Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

datagrid column to excel column

i have my data going into a datagrid.  now i need to get the data from the datagrid to an excel worksheet.  how do i get the data column to an excel column?
0
gotzcomputer
Asked:
gotzcomputer
1 Solution
 
gotzcomputerAuthor Commented:
I guess what I really want is how to copy my dataset over to an excel worksheet
0
 
computerg33kCommented:
try this:
       Dim oResizeRange As Excel.Range
        Dim oXL As Excel.Application
        Dim oWB As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        Dim oRng As Excel.Range

        'Instantiate Jobs Object
        Dim objGrabowski As New ExcelGrabowskiAttempt()
        objGrabowski.JobID = iJobID
        objGrabowski.ClientPK = iClientPK

        Try
            Dim ds As DataSet = objGrabowski.GetGrabowski
            Dim frmGrabowskiReports As New frmGrabowskiReports()

            frmGrabowskiReports.propDataSet = ds
            frmGrabowskiReports.ShowDialog()

            If frmGrabowskiReports.propGenerate Then

                Dim dt As DataTable
                dt = frmGrabowskiReports.propDataSet.Tables("Grabowski")

                'For iIndex = 0 To dt.Rows.Count - 1

                '*******************Start of Excel Sheet
                ' Start Excel and get Application object.
                oXL = CType(CreateObject("Excel.Application"), Excel.Application)
                oXL.Visible = True

                ' Get a new workbook.
                oWB = oXL.Workbooks.Add
                oSheet = CType(oWB.ActiveSheet, Excel.Worksheet)

                With oSheet.Range("A1", "W1")
                    .Font.Bold = True
                    .VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
                End With
                '*******************************************************************************************
                Dim mrow As DataRow
                Dim colindex As Integer
                Dim rowindex As Integer
                Dim col As DataColumn

                For Each col In dt.Columns
                    colindex += 1
                    oXL.Cells(1, colindex) = col.ColumnName
                Next
                rowindex = 1

                For Each mrow In dt.Rows
                    rowindex += 1
                    colindex = 0
                    For Each col In dt.Columns
                        colindex += 1
                        oXL.Cells(rowindex, colindex) = mrow(col.ColumnName).ToString()
                        objGrabowski.GetGrabowskiDetail()
                    Next
                Next
            End If
            ' AutoFit columns A.
            oRng = oSheet.Range("A23", "W23")
            oRng.EntireColumn.AutoFit()

            ' Make sure Excel is visible and give the user control
            ' of Excel's lifetime.
            oXL.Visible = True
            oXL.UserControl = True

            ' Make sure that you release object references.
            oRng = Nothing
            oSheet = Nothing
            oWB = Nothing
            oXL = Nothing

            Exit Sub
        Catch
        End Try
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now