Need some help saving excel format to disk
Hi all,
I have a situation where I need to email a formatted excel spreadsheet to a CSR when a user clicks a button on my site.
I am doing this by taking an HTML table on my site and saving that as an excel spreadsheet that gets presented to the user. I'm using the code below to do this and it works great for the formatting:
Code:
Response.AppendHeader("content-disposition", "attachment;filename=FileEName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
pnlBookingConfirmation.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
Problem is, this doesn't allow me to email the result to my CSR, it only shows it to the user.
How can I get it so that I can email it to my CSR? I was thinking I could write the file to disk and then use my existing code to send the file as an attachment, but I can't get it to properly open in Excel when I write it out.
If instead of using the response.Write and use this instead:
Code:
StreamWriter writer = File.AppendText(Server.MapPath("~/Temp/fileEName.xls"));
writer.WriteLine(sw.ToString());
writer.Close();
I get a disk file containing the table (which is what I would expect) but when I open it in excel, i get nothing displayed.
Any ideas?
Thanks,
J
Re: Need some help saving excel format to disk
Hello,
Do you "have" to send it as an attachment?
Another idea would be to create an HTML Email, and simply put the content of the Table into the body of the email?
You might want to try a writer.Flush(), before you close it.
Gary
Re: Need some help saving excel format to disk
Hi Gary,
I sorta do have to send it as an attachment, because ultimately the CSR will edit the spreadsheet and send it back to the user once the information is complete. And the CSRs aren't going to want to cut/paste into excel. Once I have the file saved I have no problem attaching it and sending it out, it's just that when the file is saved and I try to open it with excel I get nothing.
I'll add the flush as well though.
Thanks,
J
Re: Need some help saving excel format to disk
Gotcha, that makes sense.
Let me know how you get on.
Gary