Re: Export to Excell

Tech-Archive recommends: Fix windows errors by optimizing your registry



There is a good sample by Steve here at:-
http://steveorr.net/articles/ExcelExport.aspx
Patrick

"Webmills" <james@xxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1150022143.441123.33970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi

I am exporting a datagrid to Excel using the code as below. The problem
that I am experiencing is that if my datagrid includes &nbsp;
characters, spurious A with an accent characters appear within the
export. I am certain that this should be remedied by changin the
charset and/or encoding, but I cannot seem to get it right.

// Set up the page for export.
this.EnableViewState = false;
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentEncoding = Encoding.GetEncoding("utf-8");

// Set the content type.
Response.ContentType = "application/vnd.ms-excel";

// Set the title.
Response.AddHeader("content-disposition", "attachment; filename=" +
TITLE + ".xls");

// Create the string builder.
StringBuilder stringBuilder = new StringBuilder();

// Append the page start.
stringBuilder.AppendFormat("<html><head><link href=\"{0}/Styles.css\"
rel=\"style***\" type=\"text/css\" /></head><body>",
Session["RootPath"]);

// Create the html text writer.
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(new
StringWriter(stringBuilder, CultureInfo.InvariantCulture));

// Render the controls.
phrTitle.RenderControl(htmlTextWriter);
phrFinished.RenderControl(htmlTextWriter);
dgdData.RenderControl(htmlTextWriter);

// Append the page end.
stringBuilder.Append("</body></html>");

// Write the output.
Response.Write(stringBuilder.ToString());
Response.End();

Any ideas?

Regards

James



.


Quantcast