Export GridView to Excel does not open in Browser

Discussion in 'ASP .Net' started by =?Utf-8?B?bWlrZQ==?=, Jun 13, 2006.

  1. 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
     
    =?Utf-8?B?bWlrZQ==?=, Jun 13, 2006
    #1
    1. Advertising

  2. 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
     
    =?Utf-8?B?TUFDSCBJSQ==?=, Jun 14, 2006
    #2
    1. Advertising

  3. 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
     
    =?Utf-8?B?bWlrZQ==?=, Jun 15, 2006
    #3
  4. =?Utf-8?B?bWlrZQ==?=

    malilla_man

    Joined:
    Jun 12, 2007
    Messages:
    1
    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();
     
    malilla_man, Jun 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3JpZGhhcg==?=

    using Microsoft Excel image for Export to Excel button

    =?Utf-8?B?U3JpZGhhcg==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    3,160
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  2. =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=

    Export to Excel (Default File Type - Excel)

    =?Utf-8?B?SGVtYW50IFNpcGFoaW1hbGFuaQ==?=, Apr 24, 2006, in forum: ASP .Net
    Replies:
    15
    Views:
    17,658
    syed hassan
    May 21, 2009
  3. Randy
    Replies:
    1
    Views:
    342
    Randy
    Mar 7, 2007
  4. Bill E.

    Export to Excel. Browser setting?

    Bill E., Sep 7, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    844
    Bill E.
    Sep 20, 2007
  5. Grey
    Replies:
    4
    Views:
    2,112
    Mark Rae [MVP]
    Oct 17, 2007
Loading...

Share This Page