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