How To: Make "Export to Excel" always open excel in a separate Window

Rate It (8)

Last post 10-01-2009 8:46 AM by vsurana. 55 replies.

Sort Posts:

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    03-19-2008, 6:46 AM
    • Member
      4 point Member
    • sanjuvrm
    • Member since 02-26-2008, 7:42 AM
    • Posts 2

    Hi

     I have implementes above both type writen code but i am geting the error like....

    " System.Web.HttpException: Control 'GridView'Test' of type 'GridView' must be placed inside a form tag with runat=server. "

    Thanks

    Sanjay Verma

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    03-28-2008, 9:21 AM

    To avoid the IE6-specific problem of the .xls file not being found in the "temporary internet files\Content.IE5" folder, in addition to the snippets presented above, you have to do the following:

    Response.ClearHeaders();
    Response.CacheControl = "Public";

    The idea is that by default the Response had set the "no-store" attribute, and because of this immediatly after open and download the document is deleted from Temporary Internet Files.

    So what you have to do is Response.ClearHeaders() before setting your own attributes.

     

    snippet:

                        Response.ClearHeaders();
                        Response.CacheControl = "Public";
                        Response.Charset = "";
                        Response.AddHeader("Content-Disposition", "attachment; filename=file.xls");
                        Response.ContentType = "application/vnd.ms-excel";
                        Response.BinaryWrite(yourExcelStream.ToString());

     

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    03-29-2008, 12:51 AM
    • Member
      4 point Member
    • maham
    • Member since 08-07-2007, 7:34 AM
    • Posts 16

    Thanx for all of you as i got the solution by marius.poenari.

    thanx a lot

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    07-23-2008, 8:36 AM

    Hi to StrongTypes ,

                                  I have some image in the gridview when upload this to excel but there is no images found, may i know why no images uploaded into Excel file?  Please give a code lot of days i tried this task.......

    Any doubts please feel free to ask me.

    If this post is answer of your question then don't forgot to Click Mark As Answer.

    Thanks & Regards,
    J.Jeyaseelan
  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    08-14-2008, 3:11 AM
    • Member
      100 point Member
    • anubha.saxena
    • Member since 08-12-2008, 7:13 AM
    • Saxena
    • Posts 42

    using System;

    using System.IO;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Text;

    /// <summary>

    /// Summary description for Class3

    /// </summary>

    public class Class3

    {

    public Class3()

    {

     

    }

    public void Convert( DataSet ds, HttpResponse response)

    {

    //*********Export 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

     

    System.IO.
    StringWriter stringWrite =new System.IO.StringWriter();

    //create an htmltextwriter which uses the stringwriter

    HtmlTextWriter htmlWrite=new HtmlTextWriter(stringWrite);

    //instantiate a datagrid

    DataGrid dg=new 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();

    }

     

     

     

    }

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    08-14-2008, 3:27 AM

    Hai Anubha,

                    Thanks for your response. I tried this code it's working, i export the records into the excel and open it there is all images with data. if i paste this excel file to another system and open the excel file then there is no image found, could you please tell me why no images will display in another machine?

    Any doubts please feel free to ask me.

    If this post is answer of your question then don't forgot to Click Mark As Answer.

    Thanks & Regards,
    J.Jeyaseelan
  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    08-23-2008, 7:42 AM
    • Member
      3 point Member
    • happylife_80
    • Member since 03-25-2008, 1:25 PM
    • Posts 8

    Hi Ryan

    i want to export my gridview to pdf document, i used iTextSharp library, everything is fine but the problem is that

    pdf document did not take the GridView Syle like headerStyle, RowStyle, color, font...etc

    and here is my code

    Public Shared Sub FromGridView(ByVal Grid As GridView, ByVal ReportName As String)

    Dim sw As New StringWriter()

    Dim htw As New HtmlTextWriter(sw)

    Dim page As New Page

    Dim form As New HtmlForm

    Dim GridStyle As String

    page.Controls.Add(form)

    form.Controls.Add(Grid)

    form.RenderControl(htw)

    GridStyle = sw.ToString()

    'Set up the response

    HttpContext.Current.Response.Clear()

    HttpContext.Current.Response.AddHeader(
    "content-disposition", "attachment;filename=" & ReportName & Date.Now.ToShortTimeString & ".pdf")

    HttpContext.Current.Response.Charset = ""

    HttpContext.Current.Response.ContentType = "application/pdf"

    'Create pdf document

    Dim PdfDocument As New iTextSharp.text.Document(PageSize.A4)

    'Create pdf writer, output directly to OutputStream

    Dim writer As pdf.PdfWriter = pdf.PdfWriter.GetInstance(PdfDocument, HttpContext.Current.Response.OutputStream)

    PdfDocument.Open()

    'Create tempfile to hold the HTML:

    'Dim tempFile As String = Path.GetTempFileName()

    'Using tempwriter As New StreamWriter(tempFile, False)

    ' tempwriter.Write(GridStyle)

    'End Using

    Dim _xmlr As New System.Xml.XmlTextReader(New StringReader(GridStyle))

    'Parse the HTML into the document

    iTextSharp.text.html.HtmlParser.Parse(PdfDocument, _xmlr)

    'Cleanup

    PdfDocument.Close()

    writer.Close()

     

    'Delete the tempfile:

    'File.Delete(tempFile)

    writer = Nothing

    PdfDocument = Nothing

    HttpContext.Current.Response.[End]()

    End Sub

    can you help me or give me another solution

    thanx

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    08-28-2008, 4:34 AM
    • Member
      64 point Member
    • ckmurthy
    • Member since 03-11-2008, 2:12 PM
    • Posts 74

     Hi Ryan,

    I am new to asp.net ,i have one requirement ,that is uploading the HTML page into the database and  displyng  through gridview.

     if you have any code plz give me,

     

    how to do this ,plz help me.

     

    Thanks in advance

     

    Krishnamurthy C
    Consultant,
    Holmdel Consulting,
    krishnamurthy@holmdelconsulting.com
    www.holmdelconsulting.com
  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    09-05-2008, 3:43 PM
    • Member
      13 point Member
    • srinmal
    • Member since 11-27-2007, 2:52 AM
    • Posts 13

    Hello Ryan,

    Thanks for the nice post, I have samll query, my webpage where the GridView has master page  and because of this I am getting an error syaing that place inside a webform with runat=server error.

    Could you please hlep to resolve this? and also could you please send the code of clearcontrols function code in VB 

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    03-24-2009, 10:41 AM
    • Member
      27 point Member
    • Noonway
    • Member since 12-12-2006, 4:44 PM
    • Louisville, KY
    • Posts 10

    I had this same problem with IE being unable to find the file and it took me a couple of hours to figure out that I had disabled DDE in excel to prevent all my excel spreadsheets from opening up in the same window.  In Office 2007 click on Excel Options then under Advanced-->General there is an "Ignore other applications that use Dynamic Data Exchange (DDE)"  I had that option checked.  As soon as I unchecked it, the excel opened right up on the next web call.... Now to get to the formatting.

  • Re: How To: Make "Export to Excel" always open excel in a separate Window

    10-01-2009, 8:46 AM
    • Member
      28 point Member
    • vsurana
    • Member since 06-10-2008, 1:46 PM
    • Posts 24

     

    Hello

     I have an Excel File that refreshes when it opens.

    Now I need to open that File through Asp.net code and then save file and close that file.

    then Finally read data into access.

    So, how to perform the task, can anyone help.

    Please its urgent

    thanks in advance

     

Page 4 of 4 (56 items) < Previous 1 2 3 4