View MS Excel file using HttpWebRequest

R

Ryan

I have the following code that reads a file; how do I display it on the
screen or display a dialog box to save or view file?

private void LoadFile(object sender, EventArgs e)
{
try
{
MemoryStream StreamedExcelFile;
Uri uri = new
Uri("http://adoptaboxerrescue.net/protest/MyExcelFile.xls");
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(uri);

WebRequest myReq = WebRequest.Create(uri);

string sOutFile = @"c:\temp\MyFile.xls";
WebResponse wr = myReq.GetResponse();
wr.Headers.Add("Conteny-Disposition", "attachment; filename
= " + sOutFile);

Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new
StreamReader(wr.GetResponseStream(), Encoding.ASCII);
string content = reader.ReadToEnd();
}

catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
}
}
 
J

Jeff Johnson

I have the following code that reads a file; how do I display it on the
screen or display a dialog box to save or view file?

If you're downloading this file through a standard Windows program (console
app or WinForms app) then setting content headers is of no use. You simply
need to retrieve the file and then prompt the user for what to do. Setting
headers is only necessary if you're forming a response from a Web site. Then
again, you didn't provide any details as to WHERE this code is executing....

I can't imagine you'd actually want to read an Excel file as plain text, so
I think you should be using something other than StreamReader and
Encoding.ASCII. (Hint: BinaryReader)

Saving the file will be simple; you'll just display an OpenFileDialog
(assuming this is WinForms) and write the file to wherever the user
indicates. Opening Excel immediately is another matter. You'll need to save
the file somewhere (preferably a temp folder) and then use
System.Diagnostics.Process.Start(<path to the file you just saved>).
 
A

AM

Jeff Johnson said:
If you're downloading this file through a standard Windows program (console
app or WinForms app) then setting content headers is of no use. You simply
need to retrieve the file and then prompt the user for what to do. Setting
headers is only necessary if you're forming a response from a Web site. Then
again, you didn't provide any details as to WHERE this code is executing....

I can't imagine you'd actually want to read an Excel file as plain text, so
I think you should be using something other than StreamReader and
Encoding.ASCII. (Hint: BinaryReader)

Saving the file will be simple; you'll just display an OpenFileDialog
(assuming this is WinForms) and write the file to wherever the user
indicates. Opening Excel immediately is another matter. You'll need to save
the file somewhere (preferably a temp folder) and then use
System.Diagnostics.Process.Start(<path to the file you just saved>).
 
R

Ryan

It's a web app; I would like to bypass the save/open dialogbox and display
Excel file on the screen.
 
J

Jeff Johnson

It's a web app; I would like to bypass the save/open dialogbox and display
Excel file on the screen.

You can't do this. You'd have to display data in a grid or something; you
won't be able to FORCE this to appear on anyone's machine (in Excel) if they
have their machine configured to prompt for this type of file.

You can at least get to the open/save dialog by adding a content-type header
and specifying "application/vnd.ms-excel".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top