asp.net web导出Excel 下载问题
public void OutExcel(System.Data.DataTable dt){
StringWriter sw = new StringWriter();
sw.WriteLine("姓名\t手机号码\t性别\tQQ\t生日\t工作单位\t备注");
foreach (DataRow dr in dt.Rows)
{
sw.WriteLine(dr["姓名"] + "\t" + dr["手机号码"] + "\t" + dr["性别"] + "\t" + dr["QQ号"] + "\t" + dr["生日"] + "\t" + dr["工作单位"] + "\t" + dr["备注"]);
}
sw.Close();
string filename = DateTime.Now.ToString("yyyyMMddhhMMss");
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
上面是我导出Excel 的代码,客户端导出的时候出现下载的页面,下载却是整个页面。请高赐教怎么解决 下载就是我需要的内容不是整个网页 --------------------编程问答-------------------- 我导出word,也是用的二进制流方法,跟你类似.
但是没出现过导出页面问题.
代码发给你看下.
Response.AppendHeader("Content-Disposition", "attachment;filename=result.doc");
Response.ContentType = "application/ms-word";
Response.Charset = "gb2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
DataList1.Page.EnableViewState = false;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
// htmlWriter.Write(aa);
DataList1.RenderControl(htmlWriter);
Response.Write(writer.ToString());
Response.End();
补充:.NET技术 , 其他语言