Improve company productivity with a Business Account.Sign Up


datagrid column to excel column

Posted on 2004-10-29
Medium Priority
Last Modified: 2011-09-20
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?
Question by:gotzcomputer

Author Comment

ID: 12465509
I guess what I really want is how to copy my dataset over to an excel worksheet

Accepted Solution

computerg33k earned 500 total points
ID: 12506774
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

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

            frmGrabowskiReports.propDataSet = ds

            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
                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()
            End If
            ' AutoFit columns A.
            oRng = oSheet.Range("A23", "W23")

            ' 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
        End Try

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question