aspx页面
第一步
添加一个panel2 用来装生成好需要导出excel的table格式数据
<asp:Panel id="Panel2" runat="server"></asp:panel>
第二步
添加一个text_box和一个panel3
<asp:Panel id="Panel3" runat="server"></asp:Panel>
<asp:textbox id="txt_PanelInfo" style="VISIBILITY: hidden" Runat="server" Width="0px"></asp:textbox>
第三步
添加一个 javascript
<script language="javascript">
function btnOuportClick()
{ //lbl_rptdata
var InnerHtml = document.getElementById("Panel2").innerHTML.replace(/\</gm, "(*^*)")
.replace(/\>/gm,"(^*^)")
.replace(/javascript/gm, "")
.replace(/onmouseover/gm, "")
.replace(/ /gm, "")
.replace(/\,/gm, "")
.replace(/onmouseout/gm, "") ;
document.getElementById("txt_PanelInfo").value = InnerHtml;
return ;
}
</script>
第四步 添加导出按钮
<asp:Button id="Button1" runat="server" CssClass="btn3_mouseup" Text="导出"></asp:Button>
aspx.cs页面
第五步 加载button javascript 事件
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
this.Button1.Attributes.Add("onclick", "javascript:btnOuportClick();");
InitializeComponent();
base.OnInit(e);
}
第六步 按钮事件
private void Button1_Click(object sender, System.EventArgs e)
{
// Response.ContentType = "application/vnd.ms-excel";
// Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
// Response.AppendHeader("Content-Disposition", "attachment;filename=" + ddl_jd.SelectedItem.Text.ToString()+ ".xls");
// Response.Charset = "utf-8";
// this.EnableViewState = false;
// System.IO.StringWriter tw=new System.IO.StringWriter();
// System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
// this.Panel2.RenderControl(hw);
// Response.Write(tw.ToString());
// Response.End();
string aa = this.txt_PanelInfo.Text ;
aa = aa.Replace("(*^*)","<").Replace("(^*^)",">").Replace("border=0","border=1") ;
this.Panel3.Controls.Add(new LiteralControl(@aa)) ;
string filename = ddl_jd.SelectedItem.Text.ToString() ; //换成自己定义的文件名称
sqlh.OutPutExcel(Panel3, Server.UrlEncode(filename));
}
第七步 添加一个导出excel用的方法
#region Table To Excel导出
/// <summary>
/// ASP.NET 导出Excel
/// </summary>
/// <param name="dgrd">控件</param>
/// <param name="v_FileName">导出的Excel文件名</param>
public void OutPutExcel(System.Web.UI.Control control, string v_FileName )
{
System.Web.HttpContext.Current.Response.Charset = "gb2312";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ v_FileName +".xls");
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
control.Page.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
control.RenderControl(oTextWriter);
string html = oStringWriter.ToString();
string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\" />\n<style>\n</style>\n</head>\n<body>\n";
string a2 = "\n</body>\n</html>";
System.Web.HttpContext.Current.Response.Write(a1 + html + a2);
System.Web.HttpContext.Current.Response.End();
// StreamWriter sw = File.CreateText(strPath) ;
// sw.Write(Output) ;
// sw = null ;
// Response.Redirect(Server.UrlDecode(thisFullUrl)) ;
}
#endregion