这种方式出来的Excel文件不是Excel工作薄格式,要自己从新另存为一下!
StringBuilder sb = new StringBuilder();
sb.Append("<table border=1>");
for (int j = 0; j < dt.Rows.Count;j++)
{
sb.Append("<tr>");
for (int i = 1; i < dt.Columns.Count; i++)
{
sb.AppendFormat("<td>{0}</td>", dt.Rows[j][i].ToString());
}
sb.Append("</tr>");
}
sb.Append("</table>");
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.ContentType = "application/ vnd.ms-excel";
curContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");//这里写地址!现在是自动取的当前时间。
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "";
curContext.Response.Write(sb);
curContext.Response.End();