Connecting Tech Pros Worldwide Forums | Help | Site Map

How to specify filename when exporting to EXCEL

ad
Guest
 
Posts: n/a
#1: Nov 18 '05
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()

Steve C. Orr [MVP, MCSD]
Guest
 
Posts: n/a
#2: Nov 18 '05

re: How to specify filename when exporting to EXCEL


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:1EE196A9-BB9F-4A82-AB50-79F55FC9575C@microsoft.com...[color=blue]
>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()[/color]


Closed Thread