473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2408
Hi,

Excel = CreateObject("E xcel.Applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next

Next

P
.ActiveWorkbook ().SaveAs(strDi r & 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.goog legroups.com...
Hi,

Excel = CreateObject("E xcel.Applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next

Next

P
.ActiveWorkbook ().SaveAs(strDi r & 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_repor t

Excel = CreateObject("E xcel.applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To ds.Tables(0).Co lumns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToStri ng()
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString ()
Next

Next

.activeWorkbook ().SaveAs(strDi r & 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_repor t just fine,
but on the very next line for Excel = CreateObject("E xcel.applicatio n"), 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.goog legroups.com...
Hi,

Excel = CreateObject("E xcel.Applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next

Next

P
.ActiveWorkbook ().SaveAs(strDi r & 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_repor t

Excel = CreateObject("E xcel.applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To ds.Tables(0).Co lumns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToStri ng()
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString ()
Next

Next

.activeWorkbook ().SaveAs(strDi r & 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_repor t just fine,
but on the very next line for Excel = CreateObject("E xcel.applicatio n"), 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.goog legroups.com...
Hi,

Excel = CreateObject("E xcel.Applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next

Next

P
.ActiveWorkbook ().SaveAs(strDi r & 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_repor t

Excel = CreateObject("E xcel.applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To ds.Tables(0).Co lumns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToStri ng()
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString ()
Next

Next

.activeWorkbook ().SaveAs(strDi r & 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_repor t just fine,
but on the very next line for Excel = CreateObject("E xcel.applicatio n"), 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.goog legroups.com...
Hi,

Excel = CreateObject("E xcel.Applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next

Next

P
.ActiveWorkbook ().SaveAs(strDi r & 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.goo glegroups.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_repor t

Excel = CreateObject("E xcel.applicatio n")

With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1

For intColumnValue = 0 To ds.Tables(0).Co lumns.Count - 1
.cells(intRow + 1, intColumnValue + 1).value.ToStri ng()
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString ()
Next

Next

.activeWorkbook ().SaveAs(strDi r & 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_repor t just
fine,
but on the very next line for Excel = CreateObject("E xcel.applicatio n"),
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.goog legroups.com...
Hi,
>
Excel = CreateObject("E xcel.Applicatio n")
>
With Excel
.SheetsInNewWor kbook = 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).Ro ws.Count - 1
>
For intColumnValue = 0 To dt.Tables(0).Co lumns.Count - 1
.Cells(intRow + 1, intColumnValue + 1).Value =
ds.Tables(0).Ro ws(intRow).Item Array(intColumn Value).ToString
Next
>
Next
>
P
.ActiveWorkbook ().SaveAs(strDi r & 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
3102
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 to launch Excel and leave the browser as is Any help appreciate p.s. I know that this can be achieved by changing the settings on the client machine and de-selecting the 'browse in same window' for .xls extensions but this is not acceptable...
2
1778
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 would be appreciated! Michael
1
2321
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 name of excel. Can we specify a meaningful filename before exporting to EXCEL? ------------------------------------------------------------------------------------------- 'first let's clean up the response.object response.Clear()
4
6186
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
2325
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) Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Response.AppendHeader("content-disposition", "attachment;filename=test.xls") Response.Write(txtWriter.ToString) Response.End()
7
1437
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 current page on the Datgrid to Excel and if there are other pages it does not.
2
2415
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 exporting ALL data from the datagrid control. Exporting data works fine when I show all data on the datagrid control. I'd like to shows only 30 records on the datagrid control instead of ALL data using page navigation, and perform exporting...
6
2564
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 select the column orders. Instead the datasource is a class which implements IList, containing a collection of my data. My problem again is that I don't know how to control the order of the columns that are exported.
0
3444
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. When the user clicks on the Export to Excel button next to datagrid, it opens a popup with options Open, Save and Cancel. When I click open, it opens the excel sheet with the sheet name as .xls and the excel filename as temp.xls. As a result when...
0
1297
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 = oApp.Workbooks.Add Dim oSheet As Excel.Worksheet = CType(oBook.Worksheets(1), Excel.Worksheet) oApp.Visible = False
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10144
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9997
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.