Export GridView to Excel does not open in Browser

G

Guest

Hi,
I have surfed through several posting about exporting gridview to excel and
it works great. using the following code...
response.Clear()
response.ContentType = "application/vnd.ms-excel"
response.Charset = ""
Dim stringWrite As New System.IO.StringWriter()
Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)

Dim dg As New GridView()

dg.GridLines = GridLines.None
dg.HeaderStyle.Font.Bold = True
dg.Datasource = myDataTable
dg.DataBind()
'tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite)
'output the html
response.Write(stringWrite.ToString)
response.End()


However when response comes to the client it opens excel using Microsoft
Excel Application instead of inside the Browser. I have following questions...
1. How do I make it open inside the browser instead of using MS Excel
application?
2. Is there any way to explicitly instruct the browser to either open inside
browser or using MS Excel application?

Any sort of guidance on this is greatly appreciated.
Thanks
--Mike
 
G

Guest

Response.AddHeader("Content-Disposition", "filename=""report.xls""")

if this does not work check out Response.ContentType and check the mime types.

the first line should work for you, although I have used it in conjunction
with Response.BinaryWrite.

Anyways goodluck ;)
 
G

Guest

Hi Mach II,
I already tried that it does not work.
I remember somebody telling me sometime that it is client side setting that
makes a file either open in Browser or using a specific application. But
unfortunately I do not remember what exactly that is?
Thanks
--Mike
 
Joined
Jun 12, 2007
Messages
1
Reaction score
0
Hello my friend,

here is the code that are you looking for:

Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
//this.ClearControls(DataList1);
DataList1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top