在做一个用dataset生成xls文件下载的功能,在本地测试一切OK,但是传到服务器上,一点下载按钮却显示下载整个页面,哎~真让我郁闷啊,下文附有代码,请各位帮我看下,先谢了! public static void cxls(DataSet ds)
{
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode("result.xls"));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
sw.Close();
hw.Close();
HttpContext.Current.Response.End();
}
这个方法写在App_Code中view.cs类里,如果哪个页面要使用该方法,就调用它。
在服务器上运行时,当我点按钮,会弹出下载框,但是下载框里被下载的却是当前文件(如test.aspx),下载保存后打开是html代码。这和我在VS里测试的完全不一样,VS弹出来的也是一个下载框,但是下载的文件却是result.xls.
{
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode("result.xls"));
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DataGrid dg = new DataGrid();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(hw);
HttpContext.Current.Response.Write(sw.ToString());
sw.Close();
hw.Close();
HttpContext.Current.Response.End();
}
这个方法写在App_Code中view.cs类里,如果哪个页面要使用该方法,就调用它。
在服务器上运行时,当我点按钮,会弹出下载框,但是下载框里被下载的却是当前文件(如test.aspx),下载保存后打开是html代码。这和我在VS里测试的完全不一样,VS弹出来的也是一个下载框,但是下载的文件却是result.xls.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货