By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

export directly to excel file without prompting user

P: n/a
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(myHtmlTextW riter)

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

'End the response
Response.End()

Feb 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
<ma******@bellsouth.net> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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(myHtmlTextW riter)

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

'End the response
Response.End()

Feb 27 '06 #2

P: n/a
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

<ma******@bellsouth.net> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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(myHtmlTextW riter)

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

'End the response
Response.End()

Feb 28 '06 #3

P: n/a
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.
<ma******@bellsouth.net> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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(myHtmlTextW riter)

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

'End the response
Response.End()


Feb 28 '06 #4

P: n/a
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 '06 #5

P: n/a
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 '06 #6

P: n/a
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 '06 #7

P: n/a
Ya For this, Excel has to be installed on the server

Mar 1 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.