473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exporting a vb.net datagrid to excel

Hi All :-)

I'm using .Net Framework 1.1, VB and need to be able to download the data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me some
pointers? TIA,

Coleen
Sep 6 '06 #1
6 2378
Hi,

Excel = CreateObject("Excel.Application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'For displaying the column value row-by-row in the the
excel file.

For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next

Next

P
.ActiveWorkbook().SaveAs(strDir & strFileName)

.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End

it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)

Mrozu
Coleen napisal(a):
Hi All :-)

I'm using .Net Framework 1.1, VB and need to be able to download the data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me some
pointers? TIA,

Coleen
Sep 6 '06 #2
Thank you so much Mrozu!

I will try this - I appreciate your help :-)
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi,

Excel = CreateObject("Excel.Application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'For displaying the column value row-by-row in the the
excel file.

For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next

Next

P
.ActiveWorkbook().SaveAs(strDir & strFileName)

.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End

it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)

Mrozu
Coleen napisal(a):
Hi All :-)

I'm using .Net Framework 1.1, VB and need to be able to download the
data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me
some
pointers? TIA,

Coleen

Sep 6 '06 #3
Hi Mrozu :-)

I tried your code and get this error:
"Cannot create ActiveX component. "

Here is the code that I am using:
Public Sub send_to_excel()
Dim Excel As Object
Dim intRow As Integer = 0
Dim intColumnValue As Integer = 0
Dim strDir As String = ""
Dim strFilename As String = ""
Dim ds As DataSet
Dim dt As DataTable = idt_final_report

Excel = CreateObject("Excel.application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'To display the column value row-by-row in the excel file
For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToString()
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString()
Next

Next

.activeWorkbook().SaveAs(strDir & strFilename)
.activeWorkbook.close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()

End Sub

I know I haven't defined the filename or file directory, but I wanted to
test it first and see if it would even read the datatable I already have
established. It seems to set the Datatable to idt_final_report just fine,
but on the very next line for Excel = CreateObject("Excel.application"), I
get the error. Can you please explain a little more? What am I missing?
Do you know of a link that shows how to do this? Thanks so much for your
time.

Coleen
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi,

Excel = CreateObject("Excel.Application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'For displaying the column value row-by-row in the the
excel file.

For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next

Next

P
.ActiveWorkbook().SaveAs(strDir & strFileName)

.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End

it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)

Mrozu
Coleen napisal(a):
Hi All :-)

I'm using .Net Framework 1.1, VB and need to be able to download the
data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me
some
pointers? TIA,

Coleen

Sep 7 '06 #4
Hi,

so the problem is when u don't have installed Excel on your machine;)

this code is only available when you have it installed. Sorry that I
haven't told you that.

Mrozu
Coleen napisal(a):
Hi Mrozu :-)

I tried your code and get this error:
"Cannot create ActiveX component. "

Here is the code that I am using:
Public Sub send_to_excel()
Dim Excel As Object
Dim intRow As Integer = 0
Dim intColumnValue As Integer = 0
Dim strDir As String = ""
Dim strFilename As String = ""
Dim ds As DataSet
Dim dt As DataTable = idt_final_report

Excel = CreateObject("Excel.application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'To display the column value row-by-row in the excel file
For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToString()
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString()
Next

Next

.activeWorkbook().SaveAs(strDir & strFilename)
.activeWorkbook.close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()

End Sub

I know I haven't defined the filename or file directory, but I wanted to
test it first and see if it would even read the datatable I already have
established. It seems to set the Datatable to idt_final_report just fine,
but on the very next line for Excel = CreateObject("Excel.application"), I
get the error. Can you please explain a little more? What am I missing?
Do you know of a link that shows how to do this? Thanks so much for your
time.

Coleen
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi,

Excel = CreateObject("Excel.Application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'For displaying the column value row-by-row in the the
excel file.

For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next

Next

P
.ActiveWorkbook().SaveAs(strDir & strFileName)

.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End

it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)

Mrozu
Coleen napisal(a):
Hi All :-)
>
I'm using .Net Framework 1.1, VB and need to be able to download the
data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me
some
pointers? TIA,
>
Coleen
Sep 7 '06 #5
Hi,

so the problem is when u don't have installed Excel on your machine;)

this code is only available when you have it installed. Sorry that I
haven't told you that.

Mrozu
Coleen napisal(a):
Hi Mrozu :-)

I tried your code and get this error:
"Cannot create ActiveX component. "

Here is the code that I am using:
Public Sub send_to_excel()
Dim Excel As Object
Dim intRow As Integer = 0
Dim intColumnValue As Integer = 0
Dim strDir As String = ""
Dim strFilename As String = ""
Dim ds As DataSet
Dim dt As DataTable = idt_final_report

Excel = CreateObject("Excel.application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'To display the column value row-by-row in the excel file
For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToString()
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString()
Next

Next

.activeWorkbook().SaveAs(strDir & strFilename)
.activeWorkbook.close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()

End Sub

I know I haven't defined the filename or file directory, but I wanted to
test it first and see if it would even read the datatable I already have
established. It seems to set the Datatable to idt_final_report just fine,
but on the very next line for Excel = CreateObject("Excel.application"), I
get the error. Can you please explain a little more? What am I missing?
Do you know of a link that shows how to do this? Thanks so much for your
time.

Coleen
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi,

Excel = CreateObject("Excel.Application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'For displaying the column value row-by-row in the the
excel file.

For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next

Next

P
.ActiveWorkbook().SaveAs(strDir & strFileName)

.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End

it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)

Mrozu
Coleen napisal(a):
Hi All :-)
>
I'm using .Net Framework 1.1, VB and need to be able to download the
data
from a datagrid into an Excel 2000 spreadsheet for our accounting users.
Can anyone please point me to a good link on how to do this or give me
some
pointers? TIA,
>
Coleen
Sep 7 '06 #6
No that is not the problem - I DO have Excel installed on my machine - I use
it almost every day. It does not reside on a server, I actually have Excel
installed: Microsoft Excel 2000 (9.0.3926 SP-3) so that can't be the
problem. I must be missing something. Is there an Imports that I need to
include? I also had to Rem out the Option Strict and Option Explicit I had
set to On for this page. Would that make any difference?
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Hi,

so the problem is when u don't have installed Excel on your machine;)

this code is only available when you have it installed. Sorry that I
haven't told you that.

Mrozu
Coleen napisal(a):
Hi Mrozu :-)

I tried your code and get this error:
"Cannot create ActiveX component. "

Here is the code that I am using:
Public Sub send_to_excel()
Dim Excel As Object
Dim intRow As Integer = 0
Dim intColumnValue As Integer = 0
Dim strDir As String = ""
Dim strFilename As String = ""
Dim ds As DataSet
Dim dt As DataTable = idt_final_report

Excel = CreateObject("Excel.application")

With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()

'To display the column value row-by-row in the excel file
For intRow = 0 To ds.Tables(0).Rows.Count - 1

For intColumnValue = 0 To ds.Tables(0).Columns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToString()
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString()
Next

Next

.activeWorkbook().SaveAs(strDir & strFilename)
.activeWorkbook.close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()

End Sub

I know I haven't defined the filename or file directory, but I wanted to
test it first and see if it would even read the datatable I already have
established. It seems to set the Datatable to idt_final_report just
fine,
but on the very next line for Excel = CreateObject("Excel.application"),
I
get the error. Can you please explain a little more? What am I
missing?
Do you know of a link that shows how to do this? Thanks so much for
your
time.

Coleen
"Mrozu" <gr************@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
Hi,
>
Excel = CreateObject("Excel.Application")
>
With Excel
.SheetsInNewWorkbook = 1
.Workbooks.Add()
.Worksheets(1).Select()
>
'For displaying the column value row-by-row in the the
excel file.
>
For intRow = 0 To ds.Tables(0).Rows.Count - 1
>
For intColumnValue = 0 To dt.Tables(0).Columns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Rows(intRow).ItemArray(intColumnValue ).ToString
Next
>
Next
>
P
.ActiveWorkbook().SaveAs(strDir & strFileName)
>
.ActiveWorkbook.Close()
End With
Excel.Quit()
Excel = Nothing
GC.Collect()
End
>
it's a sample from my own app. it isn't very complicated so i think
that you would understand it :)
>
Mrozu
>
>
Coleen napisal(a):
Hi All :-)

I'm using .Net Framework 1.1, VB and need to be able to download the
data
from a datagrid into an Excel 2000 spreadsheet for our accounting
users.
Can anyone please point me to a good link on how to do this or give
me
some
pointers? TIA,

Coleen
>

Sep 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Neil | last post by:
Hi I'm currently exporting my datagrid to excel, taking advantage of the fact that excel can render html, the problem is that when i click the button to export it opens in browser, I want the button...
2
by: Michael | last post by:
I need to be able to export data from a web form into MS Excel. I know you can do it with a VB.net windows application, but how can you do it with an asp.net application using vb.net? Any help...
1
by: ad | last post by:
I use the code below to export the content of a data set to Excel, the code come form http://www.dotnetjohn.com/articles.aspx?articleid=36 But it always use the web form's name as the default...
4
by: Hitesh | last post by:
Hi, I have three datagrid control on my aspx page and one export to excel button, i want to export all the 3 datagrids contents in one excel file. how can i achive that? -- Thanks Hitesh
1
by: NancyA | last post by:
I am using the following code to write data from a datagrid to an Excel file: Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) dg.RenderControl(hw)...
7
by: Stephen Noronha | last post by:
Hi, I found an very interesting article on converting Datagrids to Excel. I apologize that I forgot the name of the author/article but it was very helpful. Problem: it converts only the...
2
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform...
6
by: Opa | last post by:
Hi, I have a DataGrid, whose sourceI am exporting to Excel. This works fine except for the Column ordering. My datasource is not a datatable, with a typical SELECT statement where I can...
0
by: Sridhar | last post by:
Hi, I am having trouble renaming the excel sheet while exporting to excel. we have a datagrid that contains some analytical data. I have the name to the excel file as "temp.xls" inside the code....
0
by: kanepart2 | last post by:
Hi , I have the following code to export a datagrid to excel Public Sub ExportToExcel(ByVal dt As DataTable) Try Dim oApp As New Excel.Application Dim oBook As Excel.Workbook =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.