何らかの理由でGridViewをExcelにエクスポートする際に問題が発生しました。私は2つのボタンを持っています.1つはユーザーが必要な情報を提供した後に検索を処理する検索です。もう1つのボタンは、基本的にExcelにエクスポートするグリッドビューのエクスポートを処理するエクスポートです。ExcelファイルにエクスポートするときにGridViewエクスポートでデータが返されない
私の問題は、ユーザーが検索ボタンをクリックしてからデータをエクスポートして、エクスポートボタンをクリックする必要があるということです。この時点までは、すべてが良好です.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 ParamatersとStoreProceduresを使用しています。 – jorame