Export datagrid or dataset to excel without opening it

Last post 12-21-2009 12:03 PM by sandy060583. 13 replies.

Sort Posts:

  • Export datagrid or dataset to excel without opening it

    11 hours, 19 minutes ago
    • Contributor
      2,016 point Contributor
    • kyuti
    • Member since 07-22-2008, 12:31 PM
    • Posts 472

    Hello All,

    I want to export dataset with multiple tables and datagrid to excel at perticular disk location without opening it (asking user to save).

    I have done googling but did not got much help.

    Waiting for replay.

    Thanks in advanced.



    Thanks,
    Kalpana Patel
  • Re: Export datagrid or dataset to excel without opening it

    10 hours, 52 minutes ago

    kyuti:
    I have done googling but did not got much help.
     

    And you won't get any help here to do that either. Simply put, it's not possible. Read the What ASP.NET Can And Can't Do link in my signature below.

     

    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    10 hours, 47 minutes ago
    • All-Star
      26,203 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,460

    Hi,


    have U Tried writeing  file on hard disk on Server and Try streaming It

    FileStream liveStream = new FileStream(localfilename, FileMode.Open, FileAccess.Read);

    byte[] buffer = new byte[(int)liveStream.Length];
    liveStream.Read(buffer, 0, (int)liveStream.Length);
    liveStream.Close();

    Response.Clear();
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("Content-Length", buffer.Length.ToString());
    Response.AddHeader("Content-Disposition", "attachment; filename=" + originalFilename);
    Response.BinaryWrite(buffer);
    Response.End();

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: Export datagrid or dataset to excel without opening it

    10 hours, 41 minutes ago

    have U Tried writeing file on hard disk on Server and Try streaming It

    All that will do is get the browser to open it in Excel, if Excel is installed on the user's machine

    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    10 hours, 33 minutes ago
    • All-Star
      26,203 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,460

    Mikesdotnetting:


    All that will do is get the browser to open it in Excel, if Excel is installed on the user's machine

    the we can use object tag also...

    in any case Using Response.content Type or Object tag .

    on Client there should be excel Support.

    Or


    <object type="application/vnd.ms-excel"
            data="data/test.xls" width="300" height="200">
      alt : <a href="data/test.xls">test.xls</a>
    </object>




    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: Export datagrid or dataset to excel without opening it

    7 hours, 43 minutes ago

    qwe123kids:

    the we can use object tag also...

    in any case Using Response.content Type or Object tag .

    on Client there should be excel Support.

    The OP wants a way to save the file to a location on the client's machine without the file opening or being asked to save it. Nothing you have suggested will do that. The only way to do it is to write a client-side application or activex. Both of those approaches are outside of the scope of this forum.


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    7 hours, 14 minutes ago
    • All-Star
      26,203 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,460

    Mikesdotnetting:

    The OP wants a way to save the file to a location on the client's machine without the file opening or being asked to save it. Nothing you have suggested will do that. The only way to do it is to write a client-side application or activex. Both of those approaches are outside of the scope of this forum.


    this is Not Possible either By Response.conent or <object tag>

    and ur right  he will Require ActiveX.


    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: Export datagrid or dataset to excel without opening it

    4 hours, 30 minutes ago
    • Contributor
      2,016 point Contributor
    • kyuti
    • Member since 07-22-2008, 12:31 PM
    • Posts 472

    qwe123kids:

    The OP wants a way to save the file to a location on the client's machine without the file opening or being asked to save it


    I want the excel file to be saved on server not on client machine. The requirement is like this, The sytem will have dataset and on pressing on export button it should be saved on perticulare location on server only.

    Thanks,
    Kalpana Patel
  • Re: Export datagrid or dataset to excel without opening it

    3 hours, 57 minutes ago

    Ah, sorry. That wasn't clear form your question. Put the following in your button click event:

    GridView1.DataBind();
        StringWriter writer = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
        GridView1.RenderControl(htmlWriter);
        string path = Server.MapPath("file.xls");
        if (!System.IO.File.Exists(path))
        {
          using (StreamWriter sw = System.IO.File.CreateText(path))
            sw.Write(writer.ToString());
        }

    Make sure you also add this to your code-behind:

    public override void VerifyRenderingInServerForm(Control control)
      {}

    If you need it in VB, try getting it translated at www.codechanger.com


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    3 hours, 44 minutes ago
    • Contributor
      2,016 point Contributor
    • kyuti
    • Member since 07-22-2008, 12:31 PM
    • Posts 472

    Hello Mike,

    Thanks for help. The above code worked successfully.

    But I also want to export the databse like this which is having multiple tables. I want to save each table as the sheet in same excel.

    Please help how to do ths.


    Thanks,
    Kalpana Patel
  • Re: Export datagrid or dataset to excel without opening it

    3 hours, 36 minutes ago
    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    3 hours, 29 minutes ago
    • Contributor
      2,016 point Contributor
    • kyuti
    • Member since 07-22-2008, 12:31 PM
    • Posts 472

    Hello Mike,

    Thanks for info. There isn't any other way then this like we have done with gridview?

    Thanks,
    Kalpana Patel
  • Re: Export datagrid or dataset to excel without opening it

    3 hours, 10 minutes ago

    No. The problem with the GridView generated file is that it's actually an html file. It has no concept of worksheets, so you can't create additional ones. Overall, you are better off just putting a blank worksheet on the server and writing to it. I have never been a fan of this Export GridView to Excel malarky.


    Regards Mike
    [MVP - ASP/ASP.NET]
    My site    Please help - URGENT!!!    What ASP.NET can and can't do
  • Re: Export datagrid or dataset to excel without opening it

    23 minutes ago
    • Participant
      1,294 point Participant
    • sandy060583
    • Member since 05-23-2007, 12:49 AM
    • Ahmedabad
    • Posts 327

    http://ramanisandeep.wordpress.com/2009/04/07/export-gridview-to-excel/


    http://www.dotnetjohn.com/articles.aspx?articleid=36

    Please mark the answer if it helps you

    Ramani Sandeep

    http://ramanisandeep.wordpress.com


Page 1 of 1 (14 items)