By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

How to specify filename when exporting to EXCEL

P: n/a
ad
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()
response.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.WebControls.DataGrid
'set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables(0)
'bind the datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
response.End()
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
No problem.
Use this code:
Response.AddHeader("Content-Disposition", "inline;filename=test.xls")

Here's more info:

http://www.aspnetpro.com/NewsletterA...200309so_l.asp
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"ad" <ad@discussions.microsoft.com> wrote in message
news:1E**********************************@microsof t.com...
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()
response.Charset = ""
'set the response mime type for excel
response.ContentType = "application/vnd.ms-excel"
'create a string writer
Dim stringWrite As New System.IO.StringWriter
'create an htmltextwriter which uses the stringwriter
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
'instantiate a datagrid
Dim dg As New System.Web.UI.WebControls.DataGrid
'set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables(0)
'bind the datagrid
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'all that's left is to output the html
response.Write(stringWrite.ToString)
response.End()

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.