Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Member
24 Points
27 Posts
Jan 14, 2008 06:06 AM|LINK
Hmm, not sure. Here's the method I use to write the content:
protected void WriteResponse(object content, string contentType, bool asAttachment, string filename, params object[] filenameArgs) { if (content != null) { filename = string.Format(filename, filenameArgs); bool isBinary = content.GetType().IsAssignableFrom(typeof(byte[])); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = contentType; string header = string.Format("{0}Filename={1}", asAttachment ? "attachment; " : string.Empty, filename); Response.AppendHeader("Content-Disposition", header); try { if (isBinary) { Response.AppendHeader("Content-Length", ((byte[])content).Length.ToString()); Response.Buffer = true; Response.BinaryWrite((byte[])content); } else { Response.AppendHeader("Content-Length", Encoding.Default.GetByteCount(content.ToString()).ToString()); Response.Write(content); } } catch (Exception) { Response.ClearContent(); } finally { Response.End(); } } }
Which I call like so:WriteResponse(content, "application/vnd.ms-excel", true, "Filename.xls");where content is the output of exporter.GetExportGridContent.
WriteResponse(content, "application/vnd.ms-excel", true, "Filename.xls");
timtas
Member
24 Points
27 Posts
Re: GridViewHelper and Excel Export
Jan 14, 2008 06:06 AM|LINK
Hmm, not sure. Here's the method I use to write the content:
protected void WriteResponse(object content, string contentType, bool asAttachment, string filename, params object[] filenameArgs) { if (content != null) { filename = string.Format(filename, filenameArgs); bool isBinary = content.GetType().IsAssignableFrom(typeof(byte[])); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = contentType; string header = string.Format("{0}Filename={1}", asAttachment ? "attachment; " : string.Empty, filename); Response.AppendHeader("Content-Disposition", header); try { if (isBinary) { Response.AppendHeader("Content-Length", ((byte[])content).Length.ToString()); Response.Buffer = true; Response.BinaryWrite((byte[])content); } else { Response.AppendHeader("Content-Length", Encoding.Default.GetByteCount(content.ToString()).ToString()); Response.Write(content); } } catch (Exception) { Response.ClearContent(); } finally { Response.End(); } } }