473,387 Members | 1,603 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,387 software developers and data experts.

Need a little help...

To export data from a datagrid to an excel file. I am using VB.net/ASP.net
v 2003, and not connecting to an SQL database. I have an existing datagrid
that I want to get the data from and download it to an excel file. Mrozu
was kind enough to send me some code (thank you) - unfortunately it does not
work - it gives me an error "Cannot create ActiveX component." which he
says is due to not having Excel installed on my machine. I DO have Excel on
my machine - I use it daily! It is Excel 2000, and here is the code I tried
from Mrozu:

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 must be missing something - is there an imports library class that I need?
I also tried this example from MS:
http://support.microsoft.com/kb/317719/en-us
but it is written to connect to an sql database and uses frames. Can anyone
help me decipher this for my use?

Thanks in advance,

Coleen
Sep 8 '06 #1
1 1243
Well, after much Googling and much trial and error I got this to work -
thanks very much to code by Imar Spaanjaars - here is what I did:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strwriter As New System.IO.StringWriter
Dim htmlwrite As New System.Web.UI.HtmlTextWriter(strwriter)
Response.Clear()
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Response.AddHeader("Content-Disposition", "attachment; filename=report.xls")
Dim dg As New DataGrid
dg.DataSource = dt_stat_report_5
dg.DataBind()
dg.RenderControl(htmlwrite)
Response.Write(strwriter.ToString())
Response.End()
end sub

where dt_stat_report_5 is an existing datatable that is declared publicly.

Hope this helps anyone else trying to download a datagrid to an excel file.
Also, you will only be able to save the file if you are using Excel 2000,
but you can open it directly if you have Excel 2003...

Coleen

"Coleen" <co**********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
To export data from a datagrid to an excel file. I am using
VB.net/ASP.net
v 2003, and not connecting to an SQL database. I have an existing
datagrid
that I want to get the data from and download it to an excel file. Mrozu
was kind enough to send me some code (thank you) - unfortunately it does
not
work - it gives me an error "Cannot create ActiveX component." which he
says is due to not having Excel installed on my machine. I DO have Excel
on
my machine - I use it daily! It is Excel 2000, and here is the code I
tried
from Mrozu:

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 must be missing something - is there an imports library class that I
need?
I also tried this example from MS:
http://support.microsoft.com/kb/317719/en-us
but it is written to connect to an sql database and uses frames. Can
anyone
help me decipher this for my use?

Thanks in advance,

Coleen


Sep 8 '06 #2

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

Similar topics

4
by: Marc | last post by:
Hi all, I am trying to write an application where I need the ability to open an Excel spreadsheet and do basic read/write, insert rows, and hide/unhide rows. Using win32com I have been able to...
7
by: Keyed4U | last post by:
I have a drop down list system (works to some extent) for a date range search that I need to do a little more. The following is a small sample from the script: function modify_mo_list(mo_model)...
2
by: OZ | last post by:
Hi, I am new C++ and need a little help with a public domain program that is suppose to perform a byte swap. I am receiving the following error messages during the compile process with Microsoft...
17
by: EkteGjetost | last post by:
This is definitely not the smart thing to do as far as my learning goes, but desperate situations call for desperate measures. The final lab for my introduction to C programming class is due...
5
by: Jintty | last post by:
Hi, I'm trying to write a program that will read a txt file, copy it into another text file and display the number of words, lines and paragraphs. I was able to get the copying portion done, but...
7
by: Galen Somerville | last post by:
I'm doing something wrong. I made up a test app and put the ZIP file on my website http://home.surewest.net/galen/index.html under Downloads. Basically I have a Panel that will get continuous...
12
by: nephish | last post by:
Hello there, i am getting to need to make my web stuff more OO. i have a project at work that we are porting to the internet, and i started learning php to do so. the project is now mamoth is...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
by: raypjr | last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much...
5
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
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...

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.