blah blah blah is here! blah blah » Close

up0down
link

Hi everybody,
I am trying to create a button for a web application that exports tables from a database to a excel file that can be open or saved from the site.
I've been told using headers is a good way, and the necessary header information has been provided.
header('Pragma: public');
header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=nameoffile.xls');
header('Expires: 0');
I'm currently hitting google pretty hard on the subject because I am fairly confused on how to implement "headers." Any links or a quick summary on what I need to in order to use headers would be greatly appreciated!

last answered 10 months ago

1 answers

up0down
link

// in case something else has been added previously in the pipeline
HttpContext.Current.Response.ClearHeaders();
// in case something else has been added previously in the pipeline
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Headers.Add("Content-Type", "application/vnd.ms-excel");
HttpContext.Current.Response.Headers.Add("Content-Disposition", "attachment; filename='nameoffile.xls'");
HttpContext.Current.Response.OutputStream.Write(fileBytes);
// this causes the contents to be flushed to the client immediately
// without further processing of of the Http pipeline
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();
where filebytes are the bytes from the file you're sending to the client for download.
this should work, I just did something like this a few days ago so this is from memory and I may have mistyped something.

up0down
link

Thanks!
I will definitely give and a go!

up0down
link

I added it, but I keep getting the error "fileBytes" does not exists in the current context. this makes since, so I tried in many different ways to declare a file...
File fileBytes = new File();
File fileBytes;
I'm not quite sure at what to do at this point. I'm still hitting google pretty hard.

up0down
link

I also tried...
String fileBytes;
It's says...
"No overload method "write" takes "1" arguments.

up0down
link

Oh sh*t, I think I just realized how many things are missing from this. (This is my first time using headers. I just realized it probably needs a dataSource to read from for one thing.)

up0down
link

fileBytes represents a byte array that is created from your csv data.
assume you have a string containing the excel file:
string data = "one,two,three,,four";
then you convert that to bytes:
byte[] fileBytes = Encoding.UTF8.GetBytes(data);
//... now you can pass fileBytes to the the write method
or if you had the file on the file system:
byte[] fileBytes = File.OpenReadAllBytes("myexceldoc.xls");
// ... now you can pass fileBytes to the write method
or if you're pulling data from the database you'll have to build the csv file yourself
string csvContents = "";
IDataReader reader = myQuery.ExecuteReader();
while(reader.Peek() >= 0) {
for(int i = 0; i < reader.Columns.Count; ++i) {
csvContents = reader[i].ToString() + ", ";
}
csvContents = csvContents.Trim(',', ' ');
}
byte[] fileBytes = Encoding.UTF8.GetBytes(csvContents);
// ... now you can pass fileBytes to the write method.
(ps the code above is from memory and will not work via copy / paste, but should give you a good idea about what to do.

up0down
link

thank you!

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback