在我的C#應用程序中,我從用戶的輸入生成一個Gridview,然後我必須給用戶一個選項來導出Gridview到Excel工作表。導出到Excel導出整個網頁,而不僅僅是gridview(啓用排序)
這是我abc.aspx頁:
<body>
<form id="form1" runat="server">
<div>
<b>Enter p1 :</b>
<asp:TextBox ID="tb_P1" runat="server" />
<br />
<b>Enter p2 :</b>
<asp:TextBox ID="tb_P2" runat="server" /><br />
<asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="Start Search" ClientIDMode="Static" />
<asp:Button ID="btn2" runat="server" OnClick="Button2_Click" Text="Export Data to Excel" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
ShowFooter="false"
AllowSorting="true"
OnSorting="GridView1_Sorting"
EnableViewState="false"
ShowHeaderWhenEmpty="True"
AllowPaging="false">
<RowStyle Wrap="false" />
<HeaderStyle Wrap="false" />
</asp:GridView>
</div>
</form>
<br />
</body>
這裏我的.cs頁:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Services;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.IO;
public partial class pSearch : System.Web.UI.Page
{
SqlConnection sqlconn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
DataSet dsldata;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
string fname = filename.Text;
GridView1.DataSource = (DataSet)Session["data"];
GridView1.DataBind();
int rowCount = GridView1.Rows.Count;
if (rowCount == 0)
{
Response.Write("<script>alert('Result Empty!');</script>");
}
else
{
ExportToExcel(GridView1, fname);
}
}
private void ExportToExcel(GridView GrdView, string fname)
{
try
{
Response.AddHeader("contentdisposition", "attachment;filename=test1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
GrdView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string Rname= Page.Request.QueryString["rname"];
string typeofquery = "my command";
string abc = null;
abc = "" + typeofquery + " @RName='" + RName "'";
SqlDataAdapter cmdldata = new SqlDataAdapter(abc, sqlconn);
cmdldata.SelectCommand.CommandTimeout = 600;
dsldata = new DataSet();
try
{
cmdldata.Fill(dsldata);
Session["data"] = dsldata;
GridView1.DataSource = dsldata;
GridView1.DataBind();
}//end of try
catch (Exception ex)
{
Response.Write(ex);
}//end of catch
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
dsldata = (DataSet)HttpContext.Current.Session["data"];
DataTable dt = dsldata.Tables[0];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
dt = dv.ToTable();
DataSet ds1 = new DataSet("table");
ds1.Tables.Add(dt);
Session["data"] = ds1;
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
我在GridView中排序啓用。 問題是:我的整個網頁都被導出到excel表單。我只希望將我的網格視圖導出。排序工作正常,我在Excel表格中獲得了新的排序表,但它與整個網頁一致。 我已經尋找在互聯網上的不同地方的解決方案。像許多人看到這個問題。我也嘗試了他們的解決方案,如 改變Response.ContentType =「application/vnd.xls」;到
Response.ContentType =「application/vnd.ms-excel」;和其他大多數解決方案都與我的ExportToExcel()函數類似。
自從最近2天以來,我一直在使用這個東西,但是對我的應用程序來說沒有任何工作。
請幫忙! 提前謝謝!
改變了我的ExporttoExcel功能
private void ExportToExcel(GridView GrdView, string fname)
{
try
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + fname + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
我剛剛意識到,當我在abc.aspx頁面的gridview中刪除OnSorting =「GridView1_Sorting」和AllowSorting =「true」時,我得到的只有gridview的excel表格。只有當我啓用這兩個時,問題纔會出現請幫助 ! – Ankita