export directly to excel file without prompting user

Discussion in 'ASP .Net' started by mattmerc@bellsouth.net, Feb 27, 2006.

  1. Guest

    Hi all,

    We have all seen lot of method for exporting datagrids to Excel. I have
    a slightly different need. I think it should be easy to accomplish but
    I am not sure how. I would like when a user clicks a button for
    exporting a datagrid, that the excel file is saved on the server and
    that it DOES NOT prompt the user to open or save. Here is the code I am
    using for standard export that prompts the user. Thanks all.

    Dim dgrd_export_daily_report As New Datagrid 'Create new Datagrid
    without link and paging
    dgrd_export_daily_report.Datasource = dst_export_daily_report
    dgrd_export_daily_report.Databind()
    'Tell browser to format output for Excel not IE
    Response.AddHeader( "Content-Disposition", "attachment; filename=" &
    str_file_name & "")
    ' Set the content type to Excel
    Response.ContentType = str_application

    'Turn off the view state
    Me.EnableViewState = False

    'Remove the charset from the Content-Type header
    Response.Charset = String.Empty

    Dim myTextWriter As New System.IO.StringWriter()
    Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)

    'Get the HTML for the control
    dgrd_export_daily_report.RenderControl(myHtmlTextWriter)

    'Write the HTML to the browser
    Response.Write(myTextWriter.ToString())

    'End the response
    Response.End()
    , Feb 27, 2006
    #1
    1. Advertising

  2. Norman Yuan Guest

    Where do you exactly want to save the Excel file? On ther server, as you
    mentioned in the beginning of your post, or on user computer, as your code
    shows?

    Since your code is to send data to client, of course you get prompt for
    saving/openning. Data from web server is not allowed to silently save data
    to user's computer, for safety reason.

    You need entirely different code to save the data on the server, if that is
    what you want.


    <> wrote in message
    news:...
    > Hi all,
    >
    > We have all seen lot of method for exporting datagrids to Excel. I have
    > a slightly different need. I think it should be easy to accomplish but
    > I am not sure how. I would like when a user clicks a button for
    > exporting a datagrid, that the excel file is saved on the server and
    > that it DOES NOT prompt the user to open or save. Here is the code I am
    > using for standard export that prompts the user. Thanks all.
    >
    > Dim dgrd_export_daily_report As New Datagrid 'Create new Datagrid
    > without link and paging
    > dgrd_export_daily_report.Datasource = dst_export_daily_report
    > dgrd_export_daily_report.Databind()
    > 'Tell browser to format output for Excel not IE
    > Response.AddHeader( "Content-Disposition", "attachment; filename=" &
    > str_file_name & "")
    > ' Set the content type to Excel
    > Response.ContentType = str_application
    >
    > 'Turn off the view state
    > Me.EnableViewState = False
    >
    > 'Remove the charset from the Content-Type header
    > Response.Charset = String.Empty
    >
    > Dim myTextWriter As New System.IO.StringWriter()
    > Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)
    >
    > 'Get the HTML for the control
    > dgrd_export_daily_report.RenderControl(myHtmlTextWriter)
    >
    > 'Write the HTML to the browser
    > Response.Write(myTextWriter.ToString())
    >
    > 'End the response
    > Response.End()
    >
    Norman Yuan, Feb 27, 2006
    #2
    1. Advertising

  3. It seems you want to save the HTML to a file instead of writing it out to to
    the response.
    So instead of your second to last line you should have some code that writes
    to the My.Computer.FileSystem namespace.
    Here's more info:
    http://msdn2.microsoft.com/en-us/library/0b485hf7.aspx

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net



    <> wrote in message
    news:...
    > Hi all,
    >
    > We have all seen lot of method for exporting datagrids to Excel. I have
    > a slightly different need. I think it should be easy to accomplish but
    > I am not sure how. I would like when a user clicks a button for
    > exporting a datagrid, that the excel file is saved on the server and
    > that it DOES NOT prompt the user to open or save. Here is the code I am
    > using for standard export that prompts the user. Thanks all.
    >
    > Dim dgrd_export_daily_report As New Datagrid 'Create new Datagrid
    > without link and paging
    > dgrd_export_daily_report.Datasource = dst_export_daily_report
    > dgrd_export_daily_report.Databind()
    > 'Tell browser to format output for Excel not IE
    > Response.AddHeader( "Content-Disposition", "attachment; filename=" &
    > str_file_name & "")
    > ' Set the content type to Excel
    > Response.ContentType = str_application
    >
    > 'Turn off the view state
    > Me.EnableViewState = False
    >
    > 'Remove the charset from the Content-Type header
    > Response.Charset = String.Empty
    >
    > Dim myTextWriter As New System.IO.StringWriter()
    > Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)
    >
    > 'Get the HTML for the control
    > dgrd_export_daily_report.RenderControl(myHtmlTextWriter)
    >
    > 'Write the HTML to the browser
    > Response.Write(myTextWriter.ToString())
    >
    > 'End the response
    > Response.End()
    >
    Steve C. Orr [MVP, MCSD], Feb 28, 2006
    #3
  4. Guest

    Yes, as you stated, what I need is exactly what I said I needed. :) My
    sarcasm isn't going to get me any help so I should probably
    stop...sorry.

    I gave the code example to show what I have used to export to a client
    in hopes that it only needed to be modified so I could save on the
    server. Based on your response, I guess that isn't true.


    Norman Yuan wrote:
    > Where do you exactly want to save the Excel file? On ther server, as you
    > mentioned in the beginning of your post, or on user computer, as your code
    > shows?
    >
    > Since your code is to send data to client, of course you get prompt for
    > saving/openning. Data from web server is not allowed to silently save data
    > to user's computer, for safety reason.
    >
    > You need entirely different code to save the data on the server, if that is
    > what you want.
    >
    >
    > <> wrote in message
    > news:...
    > > Hi all,
    > >
    > > We have all seen lot of method for exporting datagrids to Excel. I have
    > > a slightly different need. I think it should be easy to accomplish but
    > > I am not sure how. I would like when a user clicks a button for
    > > exporting a datagrid, that the excel file is saved on the server and
    > > that it DOES NOT prompt the user to open or save. Here is the code I am
    > > using for standard export that prompts the user. Thanks all.
    > >
    > > Dim dgrd_export_daily_report As New Datagrid 'Create new Datagrid
    > > without link and paging
    > > dgrd_export_daily_report.Datasource = dst_export_daily_report
    > > dgrd_export_daily_report.Databind()
    > > 'Tell browser to format output for Excel not IE
    > > Response.AddHeader( "Content-Disposition", "attachment; filename=" &
    > > str_file_name & "")
    > > ' Set the content type to Excel
    > > Response.ContentType = str_application
    > >
    > > 'Turn off the view state
    > > Me.EnableViewState = False
    > >
    > > 'Remove the charset from the Content-Type header
    > > Response.Charset = String.Empty
    > >
    > > Dim myTextWriter As New System.IO.StringWriter()
    > > Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)
    > >
    > > 'Get the HTML for the control
    > > dgrd_export_daily_report.RenderControl(myHtmlTextWriter)
    > >
    > > 'Write the HTML to the browser
    > > Response.Write(myTextWriter.ToString())
    > >
    > > 'End the response
    > > Response.End()
    > >
    , Feb 28, 2006
    #4
  5. Shunya Guest

    HI,
    There is a solution to your problem. You can use interop.excel and
    create excel through console application. And then the exe created by
    that application can be called by remoting through asp application.
    Thus we can avoid the prompt and can create multiple excel files
    Shunya, Feb 28, 2006
    #5
  6. Guest

    Does Excel need to be installed on the server for this to work? Thanks.
    Shunya wrote:
    > HI,
    > There is a solution to your problem. You can use interop.excel and
    > create excel through console application. And then the exe created by
    > that application can be called by remoting through asp application.
    > Thus we can avoid the prompt and can create multiple excel files
    , Feb 28, 2006
    #6
  7. Guest

    Thanks all for the help so far. I have done a lot of reading, and I
    think I need to repost my question because it looks like the need is
    slightly different now than i had originally anticiapted. We all know
    how that goes... Thanks again!
    , Feb 28, 2006
    #7
  8. Shunya Guest

    Ya For this, Excel has to be installed on the server
    Shunya, Mar 1, 2006
    #8
    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. Carlos

    authenticating without prompting

    Carlos, Sep 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    464
    Bruce Barker
    Sep 22, 2005
  2. =?Utf-8?B?a2lt?=
    Replies:
    3
    Views:
    7,557
    cbDevelopment
    Dec 15, 2005
  3. Billy Zhang

    Print directly in IE without prompting user

    Billy Zhang, Aug 5, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    840
    Steven Cheng [MSFT]
    Aug 6, 2008
  4. Jeff
    Replies:
    4
    Views:
    264
  5. pavi
    Replies:
    2
    Views:
    195
    Gregor Kofler
    Aug 17, 2007
Loading...

Share This Page