gridview 导出excel 变成了aspx页面是怎么回事 gridview 导出excel 变成了aspx页面是怎么回事 在本地导出就是.xls 的文件, 但是放在服务器下载时就变成了.aspx 页面, 求救了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 #region /// <summary> /// 根据数据控件,文件类型,文件名称导出文件 /// </summary> /// <param name="source">数据控件</param> /// <param name="type">文件类型</param> /// <param name="fileName">文件名称</param> public static void Export(Control source, string type, string fileName, Page page) { HttpResponse response = HttpContext.Current.Response; response.Clear(); response.ClearContent(); response.Buffer = true; string fType = type == "excel" ? ".xls" : ".doc"; response.HeaderEncoding = Encoding.UTF8; response.AppendHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName + fType, Encoding.UTF8)); response.ContentType = "application/ms-excel"; response.Charset = "GB2312"; response.ContentEncoding = Encoding.UTF8; StringWriter sWriter = new StringWriter(); HtmlTextWriter hWriter = new HtmlTextWriter(sWriter); source.EnableViewState = false; source.RenderControl(hWriter); response.Write(sWriter); response.ContentType = "text/html"; page.RegisterStartupScript("onload", "<script language=javascript>window.location=window.location;window.alert();</script>"); response.End(); } //重写VerifyRenderingInServerForm方法,什么事也不干(也就是阻止系统调用默认的VerifyRenderingInServerForm方法) public override void VerifyRenderingInServerForm(Control control) { } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //文本:vnd.ms-excel.numberformat:@ //日期:vnd.ms-excel.numberformat:yyyy/mm/dd //数字:vnd.ms-excel.numberformat::#,##0.00 //货币:vnd.ms-excel.numberformat¥#,##0.00 //百分比:vnd.ms-excel.numberformat:#0.00% for (int i = 0; i < e.Row.Cells.Count; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { //表示对每一行以字符串的形式输出 e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); } } } #endregion 打开EXCEL模板,遍历赋值到单元格 我的更怪,同样的Buton事件调用方法,TOEXCEL(Gridview1),有的页面可以直接下载的就是EXCEL,有的就是.ASPX,但是如果保存时在迅雷中选使用IE下载后就能下到EXCEL,我都想不通了。 http://blog.csdn.net/hongchawendu/article/details/7387535 把字符串输出为html 如何设置窗体里面的控件随窗体一起变化? ADO.net怎么实现读取某Access数据库里的所有表的表名? C# 创建windows 服务 在本地服务上怎么配置来访问它 C# 自动编号 帮帮忙呀~~怎样使GridView模板列中的textbox都能够触发textbox1_textchanged事件~~ C# 初学请教:多态是个什么意思,不甚明白!!多谢赐教 C#websocket实现客户端. 请问什么是设计模式,它和算法有什么区别和联系? 急问题:关于给自定义的控件添加事件的问题! c#反射的作用 怎样遍历一个ILIST集合 并将其所有的数据插入到表中
/// <summary>
/// 根据数据控件,文件类型,文件名称导出文件
/// </summary>
/// <param name="source">数据控件</param>
/// <param name="type">文件类型</param>
/// <param name="fileName">文件名称</param>
public static void Export(Control source, string type, string fileName, Page page)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.Buffer = true;
string fType = type == "excel" ? ".xls" : ".doc";
response.HeaderEncoding = Encoding.UTF8;
response.AppendHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName + fType, Encoding.UTF8));
response.ContentType = "application/ms-excel";
response.Charset = "GB2312";
response.ContentEncoding = Encoding.UTF8;
StringWriter sWriter = new StringWriter();
HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
source.EnableViewState = false;
source.RenderControl(hWriter);
response.Write(sWriter);
response.ContentType = "text/html";
page.RegisterStartupScript("onload", "<script language=javascript>window.location=window.location;window.alert();</script>");
response.End();
}
//重写VerifyRenderingInServerForm方法,什么事也不干(也就是阻止系统调用默认的VerifyRenderingInServerForm方法)
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//文本:vnd.ms-excel.numberformat:@
//日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//数字:vnd.ms-excel.numberformat::#,##0.00
//货币:vnd.ms-excel.numberformat¥#,##0.00
//百分比:vnd.ms-excel.numberformat:#0.00%
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//表示对每一行以字符串的形式输出
e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
}
#endregion