The server control is taking a little longer than expected, so it's just easier to paste what I pass to the helper function. Sorry if it's a little messy. I'm in the process of revamping the app where I took the code from. Basically,
in ExportGridView you pass the GridView control, the type of file you want exported, and a file name. It should work right off the bat, but if something is messed up let me know and I'll fix it.
public enum ExportType
{
CSV, Excel, Word
}
private static void ClearControls(Control control)
{
for(int index = control.Controls.Count-1; index >= 0; index--)
{
ClearControls(control.Controls[index]);
}
if(!(control is TableCell))
{
if(control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl m_Literal = new LiteralControl();
control.Parent.Controls.Add(m_Literal);
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How To: Make "Export to Excel" always open excel in a separate Window
Feb 16, 2006 03:29 AM|LINK
The server control is taking a little longer than expected, so it's just easier to paste what I pass to the helper function. Sorry if it's a little messy. I'm in the process of revamping the app where I took the code from. Basically, in ExportGridView you pass the GridView control, the type of file you want exported, and a file name. It should work right off the bat, but if something is messed up let me know and I'll fix it.
public enum ExportType
{
CSV, Excel, Word
}
private static void ClearControls(Control control)
{
for(int index = control.Controls.Count-1; index >= 0; index--)
{
ClearControls(control.Controls[index]);
}
if(!(control is TableCell))
{
if(control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl m_Literal = new LiteralControl();
control.Parent.Controls.Add(m_Literal);
m_Literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
else
{
if(control.GetType().GetProperty("Text") != null)
{
LiteralControl m_Literal = new LiteralControl();
control.Parent.Controls.Add(m_Literal);
m_Literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control,null);
control.Parent.Controls.Remove(control);
}
}
}
}
public static void ExportGridView(GridView gridView, string fileName, ExportType exportType)
{
const string m_Http_Attachment = "attachment;filename=";
const string m_Http_Content = "content-disposition";
HttpResponse m_Response = HttpContext.Current.Response;
m_Response.Clear();
m_Response.ClearContent();
m_Response.ClearHeaders();
m_Response.Buffer = true;
m_Response.AddHeader(m_Http_Content, m_Http_Attachment + fileName);
m_Response.ContentEncoding = Encoding.UTF8;
m_Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter m_StringWriter = new StringWriter();
HtmlTextWriter m_HtmlWriter = new HtmlTextWriter(m_StringWriter);
gridView.AllowPaging = false;
gridView.HeaderStyle.Font.Bold = true;
gridView.DataBind();
ClearControls(gridView);
gridView.RenderControl(m_HtmlWriter);
string m_gridViewText = m_StringWriter.ToString();
switch(exportType)
{
case ExportType.Excel:
ExportHelper.ExportToExcelWord(m_gridViewText, "application/vnd.ms-excel", m_Response);
break;
case ExportType.CSV:
ExportHelper.ExportToCsv(m_gridViewText, "application/csv", m_Response);
break;
case ExportType.Word:
ExportHelper.ExportToExcelWord(m_gridViewText, "application/vnd.ms-word", m_Response);
break;
default:
ExportHelper.ExportToExcelWord(m_gridViewText, "application/vnd.ms-excel", m_Response);
break;
}
}