Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Export GridView to Excel does not open in Browser

Reply
Thread Tools

Export GridView to Excel does not open in Browser

 
 
=?Utf-8?B?bWlrZQ==?=
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TUFDSCBJSQ==?=
Guest
Posts: n/a
 
      06-14-2006
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

"mike" wrote:

> 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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?bWlrZQ==?=
Guest
Posts: n/a
 
      06-15-2006
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


"MACH II" wrote:

> 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
>
> "mike" wrote:
>
> > 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

 
Reply With Quote
 
malilla_man malilla_man is offline
Junior Member
Join Date: Jun 2007
Posts: 1
 
      06-12-2007
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();
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to Excel (Default File Type - Excel) =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?= ASP .Net 15 05-21-2009 12:01 PM
Export to excel in asp.net using excel template Grey ASP .Net 4 10-17-2007 08:08 AM
Export to Excel. Browser setting? Bill E. ASP .Net 3 09-20-2007 04:50 PM
Export to Excel clobbers browser history (sometimes) Randy ASP .Net 1 03-07-2007 03:05 AM
using Microsoft Excel image for Export to Excel button =?Utf-8?B?U3JpZGhhcg==?= ASP .Net 0 12-09-2005 08:58 PM



Advertisments