Export to Excel

Discussion in 'ASP .Net Datagrid Control' started by ben, Jan 21, 2004.

  1. ben

    ben Guest

    Hi,
    I have an aspx page with an export to excel functionality....it opens
    a dialog box asking the client to save/open the xls file.However this
    works fine on my machine but does not work on another PC and
    onclicking the export button( where all the code is written)....it
    does nothing.....i am pasting the code below...any info will be useful
    Also, one of the columns in the datagrid is 16 charasters long(it is
    numberic data but stored as varchar in DB); it gets converted to
    numeric form in the spreadsheet..how do i make it as text....
    Thanks in advance for any inputs

    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Clear();
    Response.Buffer=true;
    Response.ContentType="application/vnd.ms-excel";

    //Response.AddHeader("content-disposition","attachment;filename=myfile.xls";
    Response.AddHeader("content-disposition","attachment;filename=myfile"+"_"+DateTime.Now.Day.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Year.ToString()+"_"+DateTime.Now.Hour.ToString()+"_"+DateTime.Now.Minute.ToString()+"_"+DateTime.Now.Second.ToString()+".xls");
    Response.Charset="";
    this.EnableViewState=false;


    System.IO.StringWriter SW= new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw= new HtmlTextWriter(SW);


    string connstr="";
    string selecttext="";
    SqlConnection myconnection=new SqlConnection(connstr);
    SqlCommand cmd = new SqlCommand(selecttext,myconnection);
    myconnection.Open();
    SqlDataAdapter da= new SqlDataAdapter();
    da.SelectCommand=cmd;
    DataTable myds= new DataTable();
    da.Fill(myds);
    DataGrid dg= new DataGrid();
    dg.DataSource= myds;
    dg.DataBind();
    //this.ClearControls(dg);
    dg.RenderControl(hw);

    Response.Write(SW.ToString());
    Response.End();
    // Put user code to initialize the page here
    }


    This is the code in the aspx page which will open on clicking the
    export to excel button on the main aspx page.....however nothign will
    get displayed in this and the dialog box to save/open will directly
    pop up....

    P.S Checked the folder options to be accurate(for XLS format)

    Thanks again

    Ben Arthur
     
    ben, Jan 21, 2004
    #1
    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?RGF2aWQgVmFsbGU=?=

    Invalid export DLL or export format

    =?Utf-8?B?RGF2aWQgVmFsbGU=?=, Oct 29, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    3,640
    =?Utf-8?B?RGF2aWQgVmFsbGU=?=
    Oct 29, 2003
  2. =?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,135
    =?Utf-8?B?U3JpZGhhcg==?=
    Dec 9, 2005
  3. =?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,581
    syed hassan
    May 21, 2009
  4. Grey
    Replies:
    4
    Views:
    2,045
    Mark Rae [MVP]
    Oct 17, 2007
  5. Maarten Porters
    Replies:
    1
    Views:
    436
    Florian Gilcher
    Jul 28, 2008
Loading...

Share This Page