toExcel.aspx 主要是将数据导出到Excel中
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
public partial class pagination_toExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ReportName = Request.QueryString["excelTitle"];
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(ReportName+".xls", Encoding.UTF8).ToString());
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
string head = @"<html><head><meta http-equiv=Content-Type content=text/html; charset=utf-8><style>td{mso-number-format:\@}</style></head><body>";
string foot = "</body></html>";
hw.Write("<table ><tr align=center><td align=center colspan=10 style=font-size:14pt >" + ReportName + "</td></tr></table><br>");
GridView gv = new GridView();
gv.GridLines = GridLines.Both;
gv.DataSource = ToExcel();
gv.DataBind();
gv.RenderControl(hw);
Response.Write(head + tw.ToString() + foot);
Response.End();
}
public DataTable ToExcel()
{
StringBuilder str=new StringBuilder();
str.Append("select ")
.Append(Request.QueryString["excelSql"])
.Append(" from ")
.Append(Request.QueryString["tableName"])
.Append(Request.QueryString["where"] == "null" ? "" : " where " + Request.QueryString["where"])
.Append(" order by ")
.Append(Request.QueryString["sort"])
.Append(" ")
.Append(Request.QueryString["dir"] == "ASC" ? "ASC" : "DESC");
return CHY.DbHelperSQL.GetDataTable(str.ToString());
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}