0
從我的網站上,我有一個按鈕,它調用一個方法,然後生成excel報告。代碼起作用。問題是代碼在同一個選項卡上顯示所有gridviews(有3個)。我可以添加什麼代碼,以便當我點擊按鈕時,它將下載文檔,但每個gridview都顯示在其單獨的選項卡上。標籤名稱將是top 1,top 2和top 3.爲了澄清,現在所有的gridviews都顯示在top 1選項卡上,我需要製作2個以上的選項卡(top 2和top 3),並將gridview2在頂部2選項卡上,以及頂部3選項卡上的gridview3。只有1個數據集。我可以使用什麼代碼來循環它,以便將每個表格顯示到不同的工作表中?將gridviews導出爲excel進入3個工作表
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ISALog1ConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("exec ProxyReport", conn);
cmd.CommandTimeout = 200;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dataSet);
GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
GridView2.DataSource = dataSet.Tables[1];
GridView2.DataBind();
GridView3.DataSource = dataSet.Tables[2];
GridView3.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=Top 1.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
GridView2.RenderControl(htw);
GridView3.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
}
我不知道準確的答案,但我會想象它會涉及到像在每個'GridView.RenderControl'的輸出中包含''標記的內容。除非我錯過了一些東西,否則輸出不應該是'.xlsx'文件? –
2012-07-12 16:16:17
是的,它應該是一個xlsx。但即時通訊我想我需要改變很多代碼...並添加一些循環代碼。並替換button1_click方法的內容..我只是不知道如何.. – Cloud 2012-07-12 16:29:28