1
我出於某種原因導致將GridView導出到Excel時出現問題。我有兩個按鈕,一個是搜索,一旦用戶提供了所需的信息,就會處理搜索。另一個按鈕是基本上處理gridview出口excel的導出。導出到Excel文件時,GridView導出不會返回任何數據
我的問題是,當用戶點擊搜索按鈕,然後他們想要將數據導出爲Excel時,他們需要點擊導出按鈕。一切都很好,直到這一點,當Excel文件是查看,沒有數據導出。這裏是我的兩個按鈕的代碼:
任何幫助將不勝感激,謝謝。
protected void search(object sender, EventArgs e)
{
odbc.Open();
ds = new DataSet();
cmd = new OdbcCommand("SELECT XHLBCD AS LOCATION, XHLCST AS STATUS, XHEXUN AS EXCESS, XHSHUN AS SHORT, XHCNTD AS DATE_COUNTED FROM " +
"WM242BASD.XHCTRL00 WHERE XHCNTD BETWEEN '" + fromdate.Text + "' AND '" + todate.Text + "'", odbc);
cmd.CommandType = CommandType.Text;
cmd.Connection = odbc;
oda = new OdbcDataAdapter(cmd);
oda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
odbc.Close();
}
protected void export_OnClick(object sender, EventArgs e)
{
// Let's hide all unwanted stuffing
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
// Let's bind data to GridView
BindGrid();
//Change the color back to white
GridView1.HeaderRow.Style.Add("background-color", "#ffffff");
//Apply color to the header
GridView1.HeaderRow.Cells[0].Style.Add("background-color", "#e0e0e0");
GridView1.HeaderRow.Cells[1].Style.Add("background-color", "#e0e0e0");
GridView1.HeaderRow.Cells[2].Style.Add("background-color", "#e0e0e0");
GridView1.HeaderRow.Cells[3].Style.Add("background-color", "#e0e0e0");
GridView1.HeaderRow.Cells[4].Style.Add("background-color", "#e0e0e0");
// Let's output the GridView
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls");
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
GridView1.RenderControl(hwriter);
Response.Write(swriter.ToString());
Response.End();
}
private void BindGrid()
{
GridView1.DataBind();
}
謝謝。這工作,是的,我使用這種方式進行測試,只有一次我進入生產我使用SQL參數和StoreProcedures – jorame 2012-02-07 19:50:47