Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,257 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,298 people online right now. Registration is fast and FREE... Join Now!




Datagrid to an Excel Application

 
Reply to this topicStart new topic

Datagrid to an Excel Application

bushyt
29 Aug, 2007 - 03:07 AM
Post #1

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 3



Thanked: 1 times
My Contributions
Hi Guys.

I'm New in Vb.Net and I really Need your Help Guys.
I'm trying to Export Data from a Data grid to Excel,
Does any one of you have a Code to do that?

Can you please Help me with that?
Thanking you in Anticipation for your Support and Understanding


User is offlineProfile CardPM
+Quote Post


PsychoCoder
RE: Datagrid To An Excel Application
29 Aug, 2007 - 03:42 AM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,276



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
There is some ideas on doing exactly that here

Happy Coding!
User is offlineProfile CardPM
+Quote Post

bushyt
RE: Datagrid To An Excel Application
29 Aug, 2007 - 04:35 AM
Post #3

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 3



Thanked: 1 times
My Contributions
QUOTE(bushyt @ 29 Aug, 2007 - 04:07 AM) *

Hi Guys.

I'm New in Vb.Net and I really Need your Help Guys.
I'm trying to Export Data from a Data grid to Excel,
Does any one of you have a Code to do that?

Can you please Help me with that?
Thanking you in Anticipation for your Support and Understanding



Thanks a lot, but like i said i have never used VB.Net before so i was looking for something straight and forward (Code) can you help me with that?

Thanks a Lot
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Datagrid To An Excel Application
29 Aug, 2007 - 04:36 AM
Post #4

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,276



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Read the example I posted, try to implement it, when you run into problems/errors post the code you are working with and we will try and help you, I am not, however, going to just post a solution to your problem so you don't have to do anything except copy & paste to get it working smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Datagrid To An Excel Application
29 Aug, 2007 - 11:10 AM
Post #5

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,276



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
bushyt, I just had to write functionality in an application to do just this, play around with the link I sent you, see what you can come up with, then once you do I can start showing you how I accomplished it smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Datagrid To An Excel Application
29 Aug, 2007 - 02:08 PM
Post #6

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,276



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
To export data to Excel I use the following method. THis method takes your DataTable (which is what I assume you're binding to your DataGridView) and populates an Excel file with it. To make this work you need to add a reference to the Microsoft.Excel11 Interop (or 12 for 2007, 10 for XP, etc). To do this follow these steps
  1. Click Project on the menu
  2. Select Add Reference from the drop down menu
  3. Once the dialog opens click the COM tab
  4. Scroll down to Microsoft Excel 12.0 (or whatever version you're running) and highlight it
  5. Click OK

Once you've done that, add the following import statements to your file

CODE

Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel


I have a function and a sub procedure for this, one to actually create the Excel file, and one to dump the data from the DataTable into the Excel file. The function, aptly named CreateExcelFile takes 2 parameters, the name you want to save the file as and the DataTable that contains your data.

CODE

Public Shared Function CreateExcelFile(fileName as String, dt As DataTable) As Boolean
    Dim excelExport As New Microsoft.Office.Interop.Excel.Application()
    Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
    Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets
    Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim excelCells As Microsoft.Office.Interop.Excel.Range
    Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")
    Dim exPath As String = System.Windows.Forms.Application.ExecutablePath
    Dim filePath As String
    Try
        If Not isInstalled(ApiEnums.OfficeApplications.Excel) Then
              MsgBox("Microsoft Excel is required for this functionality." & vbCrLf & _
              "Contact your Help Desk about getting " & vbCrLf & "this installed. Thank You.")
        Else
              filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName
              If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                   Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
              End If
              excelExport.Visible = False : excelExport.DisplayAlerts = False
              excelBook = excelExport.Workbooks.Add
              excelSheets = excelBook.Worksheets
              excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
              excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") &

Date.Now.ToString("yy")
              excelCells = excelSheet.Cells

              PopulateSheet(dt, excelCells)

              excelSheet.SaveAs(filePath)
              excelBook.Close()
              excelExport.Quit()

              ReleaseComObject(excelCells) : ReleaseComObject(excelSheet) :

ReleaseComObject(excelSheets)
              ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

              excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing
              excelSheet = Nothing : excelCells = Nothing

              System.GC.Collect()
              Return True
       End If
   Catch ex As Exception
       MsgBox(ex.Message,"Error exporting data")
       Return False
   End Try
End Function


Now the PopulateSheet procedure

CODE

Private Shared Sub PopulateSheet(ByVal dt As System.Data.DataTable, ByVal oCells As

Microsoft.Office.Interop.Excel.Range)
    Dim dRow As DataRow
    Dim dataArray() As Object
    Dim count As Integer
    Dim column_count As Integer
    'Output Column Headers
    For column_count = 0 To dt.Columns.Count - 1
        oCells(2, column_count + 1) = dt.Columns(column_count).ToString
    Next
    'Output Data
    For count = 0 To dt.Rows.Count - 1
        dRow = dt.Rows.Item(count)
        dataArray = dRow.ItemArray
        For column_count = 0 To UBound(dataArray)
            oCells(count + 3, column_count + 1) = dataArray(column_count).ToString
        Next
    Next
End Sub


There are other ways to accomplish this task, this is just one of many ways.

Hope this helps smile.gif

Happy Coding!
User is offlineProfile CardPM
+Quote Post

bushyt
RE: Datagrid To An Excel Application
30 Aug, 2007 - 12:52 AM
Post #7

New D.I.C Head
*

Joined: 29 Aug, 2007
Posts: 3



Thanked: 1 times
My Contributions
QUOTE(PsychoCoder @ 29 Aug, 2007 - 03:08 PM) *

To export data to Excel I use the following method. THis method takes your DataTable (which is what I assume you're binding to your DataGridView) and populates an Excel file with it. To make this work you need to add a reference to the Microsoft.Excel11 Interop (or 12 for 2007, 10 for XP, etc). To do this follow these steps
  1. Click Project on the menu
  2. Select Add Reference from the drop down menu
  3. Once the dialog opens click the COM tab
  4. Scroll down to Microsoft Excel 12.0 (or whatever version you're running) and highlight it
  5. Click OK
Once you've done that, add the following import statements to your file

CODE

Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel


I have a function and a sub procedure for this, one to actually create the Excel file, and one to dump the data from the DataTable into the Excel file. The function, aptly named CreateExcelFile takes 2 parameters, the name you want to save the file as and the DataTable that contains your data.

CODE

Public Shared Function CreateExcelFile(fileName as String, dt As DataTable) As Boolean
    Dim excelExport As New Microsoft.Office.Interop.Excel.Application()
    Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
    Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets
    Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim excelCells As Microsoft.Office.Interop.Excel.Range
    Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")
    Dim exPath As String = System.Windows.Forms.Application.ExecutablePath
    Dim filePath As String
    Try
        If Not isInstalled(ApiEnums.OfficeApplications.Excel) Then
              MsgBox("Microsoft Excel is required for this functionality." & vbCrLf & _
              "Contact your Help Desk about getting " & vbCrLf & "this installed. Thank You.")
        Else
              filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName
              If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                   Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
              End If
              excelExport.Visible = False : excelExport.DisplayAlerts = False
              excelBook = excelExport.Workbooks.Add
              excelSheets = excelBook.Worksheets
              excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
              excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") &

Date.Now.ToString("yy")
              excelCells = excelSheet.Cells

              PopulateSheet(dt, excelCells)

              excelSheet.SaveAs(filePath)
              excelBook.Close()
              excelExport.Quit()

              ReleaseComObject(excelCells) : ReleaseComObject(excelSheet) :

ReleaseComObject(excelSheets)
              ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

              excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing
              excelSheet = Nothing : excelCells = Nothing

              System.GC.Collect()
              Return True
       End If
   Catch ex As Exception
       MsgBox(ex.Message,"Error exporting data")
       Return False
   End Try
End Function


Now the PopulateSheet procedure

CODE

Private Shared Sub PopulateSheet(ByVal dt As System.Data.DataTable, ByVal oCells As

Microsoft.Office.Interop.Excel.Range)
    Dim dRow As DataRow
    Dim dataArray() As Object
    Dim count As Integer
    Dim column_count As Integer
    'Output Column Headers
    For column_count = 0 To dt.Columns.Count - 1
        oCells(2, column_count + 1) = dt.Columns(column_count).ToString
    Next
    'Output Data
    For count = 0 To dt.Rows.Count - 1
        dRow = dt.Rows.Item(count)
        dataArray = dRow.ItemArray
        For column_count = 0 To UBound(dataArray)
            oCells(count + 3, column_count + 1) = dataArray(column_count).ToString
        Next
    Next
End Sub


There are other ways to accomplish this task, this is just one of many ways.

Hope this helps smile.gif

Happy Coding!



QUOTE(bushyt @ 30 Aug, 2007 - 01:48 AM) *

QUOTE(PsychoCoder @ 29 Aug, 2007 - 03:08 PM) *

To export data to Excel I use the following method. THis method takes your DataTable (which is what I assume you're binding to your DataGridView) and populates an Excel file with it. To make this work you need to add a reference to the Microsoft.Excel11 Interop (or 12 for 2007, 10 for XP, etc). To do this follow these steps
  1. Click Project on the menu
  2. Select Add Reference from the drop down menu
  3. Once the dialog opens click the COM tab
  4. Scroll down to Microsoft Excel 12.0 (or whatever version you're running) and highlight it
  5. Click OK
Once you've done that, add the following import statements to your file

CODE

Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel


I have a function and a sub procedure for this, one to actually create the Excel file, and one to dump the data from the DataTable into the Excel file. The function, aptly named CreateExcelFile takes 2 parameters, the name you want to save the file as and the DataTable that contains your data.

CODE

Public Shared Function CreateExcelFile(fileName as String, dt As DataTable) As Boolean
    Dim excelExport As New Microsoft.Office.Interop.Excel.Application()
    Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
    Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets
    Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim excelCells As Microsoft.Office.Interop.Excel.Range
    Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")
    Dim exPath As String = System.Windows.Forms.Application.ExecutablePath
    Dim filePath As String
    Try
        If Not isInstalled(ApiEnums.OfficeApplications.Excel) Then
              MsgBox("Microsoft Excel is required for this functionality." & vbCrLf & _
              "Contact your Help Desk about getting " & vbCrLf & "this installed. Thank You.")
        Else
              filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName
              If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                   Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
              End If
              excelExport.Visible = False : excelExport.DisplayAlerts = False
              excelBook = excelExport.Workbooks.Add
              excelSheets = excelBook.Worksheets
              excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
              excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") &

Date.Now.ToString("yy")
              excelCells = excelSheet.Cells

              PopulateSheet(dt, excelCells)

              excelSheet.SaveAs(filePath)
              excelBook.Close()
              excelExport.Quit()

              ReleaseComObject(excelCells) : ReleaseComObject(excelSheet) :

ReleaseComObject(excelSheets)
              ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

              excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing
              excelSheet = Nothing : excelCells = Nothing

              System.GC.Collect()
              Return True
       End If
   Catch ex As Exception
       MsgBox(ex.Message,"Error exporting data")
       Return False
   End Try
End Function


Now the PopulateSheet procedure

CODE

Private Shared Sub PopulateSheet(ByVal dt As System.Data.DataTable, ByVal oCells As

Microsoft.Office.Interop.Excel.Range)
    Dim dRow As DataRow
    Dim dataArray() As Object
    Dim count As Integer
    Dim column_count As Integer
    'Output Column Headers
    For column_count = 0 To dt.Columns.Count - 1
        oCells(2, column_count + 1) = dt.Columns(column_count).ToString
    Next
    'Output Data
    For count = 0 To dt.Rows.Count - 1
        dRow = dt.Rows.Item(count)
        dataArray = dRow.ItemArray
        For column_count = 0 To UBound(dataArray)
            oCells(count + 3, column_count + 1) = dataArray(column_count).ToString
        Next
    Next
End Sub


There are other ways to accomplish this task, this is just one of many ways.

Hope this helps smile.gif

Happy Coding!





Thanks a Lot, this really Helped me but i'm still working on it to suite what i'm busy doing.
Once i'm done with everything i'll let you know.
Thanks again.
User is offlineProfile CardPM
+Quote Post

Westwood
RE: Datagrid To An Excel Application
20 Sep, 2007 - 06:07 AM
Post #8

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 1


My Contributions
QUOTE(bushyt @ 30 Aug, 2007 - 01:52 AM) *

QUOTE(PsychoCoder @ 29 Aug, 2007 - 03:08 PM) *

To export data to Excel I use the following method. THis method takes your DataTable (which is what I assume you're binding to your DataGridView) and populates an Excel file with it. To make this work you need to add a reference to the Microsoft.Excel11 Interop (or 12 for 2007, 10 for XP, etc). To do this follow these steps
  1. Click Project on the menu
  2. Select Add Reference from the drop down menu
  3. Once the dialog opens click the COM tab
  4. Scroll down to Microsoft Excel 12.0 (or whatever version you're running) and highlight it
  5. Click OK
Once you've done that, add the following import statements to your file

CODE

Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel


I have a function and a sub procedure for this, one to actually create the Excel file, and one to dump the data from the DataTable into the Excel file. The function, aptly named CreateExcelFile takes 2 parameters, the name you want to save the file as and the DataTable that contains your data.

CODE

Public Shared Function CreateExcelFile(fileName as String, dt As DataTable) As Boolean
    Dim excelExport As New Microsoft.Office.Interop.Excel.Application()
    Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
    Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets
    Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim excelCells As Microsoft.Office.Interop.Excel.Range
    Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")
    Dim exPath As String = System.Windows.Forms.Application.ExecutablePath
    Dim filePath As String
    Try
        If Not isInstalled(ApiEnums.OfficeApplications.Excel) Then
              MsgBox("Microsoft Excel is required for this functionality." & vbCrLf & _
              "Contact your Help Desk about getting " & vbCrLf & "this installed. Thank You.")
        Else
              filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName
              If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                   Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
              End If
              excelExport.Visible = False : excelExport.DisplayAlerts = False
              excelBook = excelExport.Workbooks.Add
              excelSheets = excelBook.Worksheets
              excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
              excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") &

Date.Now.ToString("yy")
              excelCells = excelSheet.Cells

              PopulateSheet(dt, excelCells)

              excelSheet.SaveAs(filePath)
              excelBook.Close()
              excelExport.Quit()

              ReleaseComObject(excelCells) : ReleaseComObject(excelSheet) :

ReleaseComObject(excelSheets)
              ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

              excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing
              excelSheet = Nothing : excelCells = Nothing

              System.GC.Collect()
              Return True
       End If
   Catch ex As Exception
       MsgBox(ex.Message,"Error exporting data")
       Return False
   End Try
End Function


Now the PopulateSheet procedure

CODE

Private Shared Sub PopulateSheet(ByVal dt As System.Data.DataTable, ByVal oCells As

Microsoft.Office.Interop.Excel.Range)
    Dim dRow As DataRow
    Dim dataArray() As Object
    Dim count As Integer
    Dim column_count As Integer
    'Output Column Headers
    For column_count = 0 To dt.Columns.Count - 1
        oCells(2, column_count + 1) = dt.Columns(column_count).ToString
    Next
    'Output Data
    For count = 0 To dt.Rows.Count - 1
        dRow = dt.Rows.Item(count)
        dataArray = dRow.ItemArray
        For column_count = 0 To UBound(dataArray)
            oCells(count + 3, column_count + 1) = dataArray(column_count).ToString
        Next
    Next
End Sub


There are other ways to accomplish this task, this is just one of many ways.

Hope this helps smile.gif

Happy Coding!



QUOTE(bushyt @ 30 Aug, 2007 - 01:48 AM) *

QUOTE(PsychoCoder @ 29 Aug, 2007 - 03:08 PM) *

To export data to Excel I use the following method. THis method takes your DataTable (which is what I assume you're binding to your DataGridView) and populates an Excel file with it. To make this work you need to add a reference to the Microsoft.Excel11 Interop (or 12 for 2007, 10 for XP, etc). To do this follow these steps
  1. Click Project on the menu
  2. Select Add Reference from the drop down menu
  3. Once the dialog opens click the COM tab
  4. Scroll down to Microsoft Excel 12.0 (or whatever version you're running) and highlight it
  5. Click OK
Once you've done that, add the following import statements to your file

CODE

Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel


I have a function and a sub procedure for this, one to actually create the Excel file, and one to dump the data from the DataTable into the Excel file. The function, aptly named CreateExcelFile takes 2 parameters, the name you want to save the file as and the DataTable that contains your data.

CODE

Public Shared Function CreateExcelFile(fileName as String, dt As DataTable) As Boolean
    Dim excelExport As New Microsoft.Office.Interop.Excel.Application()
    Dim excelBook As Microsoft.Office.Interop.Excel.Workbook
    Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets
    Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim excelCells As Microsoft.Office.Interop.Excel.Range
    Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")
    Dim exPath As String = System.Windows.Forms.Application.ExecutablePath
    Dim filePath As String
    Try
        If Not isInstalled(ApiEnums.OfficeApplications.Excel) Then
              MsgBox("Microsoft Excel is required for this functionality." & vbCrLf & _
              "Contact your Help Desk about getting " & vbCrLf & "this installed. Thank You.")
        Else
              filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName
              If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                   Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
              End If
              excelExport.Visible = False : excelExport.DisplayAlerts = False
              excelBook = excelExport.Workbooks.Add
              excelSheets = excelBook.Worksheets
              excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
              excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") &

Date.Now.ToString("yy")
              excelCells = excelSheet.Cells

              PopulateSheet(dt, excelCells)

              excelSheet.SaveAs(filePath)
              excelBook.Close()
              excelExport.Quit()

              ReleaseComObject(excelCells) : ReleaseComObject(excelSheet) :

ReleaseComObject(excelSheets)
              ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

              excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing
              excelSheet = Nothing : excelCells = Nothing

              System.GC.Collect()
              Return True
       End If
   Catch ex As Exception
       MsgBox(ex.Message,"Error exporting data")
       Return False
   End Try
End Function


Now the PopulateSheet procedure

CODE

Private Shared Sub PopulateSheet(ByVal dt As System.Data.DataTable, ByVal oCells As

Microsoft.Office.Interop.Excel.Range)
    Dim dRow As DataRow
    Dim dataArray() As Object
    Dim count As Integer
    Dim column_count As Integer
    'Output Column Headers
    For column_count = 0 To dt.Columns.Count - 1
        oCells(2, column_count + 1) = dt.Columns(column_count).ToString
    Next
    'Output Data
    For count = 0 To dt.Rows.Count - 1
        dRow = dt.Rows.Item(count)
        dataArray = dRow.ItemArray
        For column_count = 0 To UBound(dataArray)
            oCells(count + 3, column_count + 1) = dataArray(column_count).ToString
        Next
    Next
End Sub


There are other ways to accomplish this task, this is just one of many ways.

Hope this helps smile.gif

Happy Coding!





Thanks a Lot, this really Helped me but i'm still working on it to suite what i'm busy doing.
Once i'm done with everything i'll let you know.
Thanks again.


I'm having some issue with this part: isInstalled(ApiEnums.OfficeApplications.Excel)

Is there a reference or an import I should be using to get isInstalled to be recognized? Also I'm assuming that ApiEnums is something that you wrote. Would you be able to post that as well?


User is offlineProfile CardPM
+Quote Post

webwired
RE: Datagrid To An Excel Application
16 Dec, 2007 - 09:44 AM
Post #9

D.I.C Head
Group Icon

Joined: 26 Aug, 2007
Posts: 199



Thanked: 3 times
Dream Kudos: 100
My Contributions
QUOTE
CODE
            If Not Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then
                Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")
            End If


I get "Name 'Directory' is not declared."

I have spent three days googling, reading forums and tutorials, I can't wait to find out if this actually works... but I'm not sure if this code requires another import or ???
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 11:06AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month