日期:2014-05-17  浏览次数:20359 次

数据的导出
我在页面中用table划的表格,我想把它导出到excel。代码怎么写?请教了

------解决方案--------------------
参考:
http://www.cnblogs.com/insus/articles/1400266.html
------解决方案--------------------
用asp:table可直接导出数据,再给你个方法,
//导出 DataTable 到 Excel
public void OutExcel(Table dg, string name, HttpResponse Response)
{
// dg.Visible = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
name = "attachment;filename=" + name;
Response.AppendHeader("Content-Disposition", name);
Response.ContentEncoding = System.Text.Encoding.GetEncoding(65001);
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
Response.ContentType = "application/ms-excel";
// Response.ContentType = "application/vnd.word";
dg.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}