使用c#导出到Excel(在Web应用程序中)

时间:2010-08-26 11:04:58

标签: c# asp.net download export-to-excel

我已经完成了从Gridview到Excel工作表的导出到Excel代码。我有下载对话框。如何获取按钮值,无论是保存数据还是下载或按下取消按钮关闭对话框.....

我想在下载对话框中确定按下的按钮是保存还是取消。

代码:

public void Export(GridView GridView1,DataTable dtGuid)     {

        string attachment = "attachment; filename=ScratchcardDetails.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", attachment);

        Response.ContentType = "application/ms-excel";


        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.Visible = true;
        GridView1.DataSource = dtGuid;
        GridView1.DataBind();


        // Create a form to contain the grid

        HtmlForm frm = new HtmlForm();
        this.GridView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(this.GridView1);
        frm.RenderControl(htw);
        //Response.Write(style);
        Response.Write(sw.ToString());
        Response.End();


}

1 个答案:

答案 0 :(得分:0)

SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
    // Save hit
}