在asp.net中将Gridview数据导出到Excel工作表 [英] Exporting Gridview Data to Excel Sheet in asp.net

查看:120
本文介绍了在asp.net中将Gridview数据导出到Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已使用以下代码将gridiew数据导出到Excel.但是有一个小问题,同时导出控件也是可见的.请问有人可以建议在导出数据后如何禁用Excel工作表中的控件吗?

Hi All,

I have used the following code for exporting the gridiew data to Excel. But there is a small problem, while exporting the controls are also visible. Can any one please suggest how to disable the controls in the excel sheet after export of data

protected void btnGetData_Click(object sender, EventArgs e)
    {
        try
        {
            GetDataDisplay();
            btnExportExcel.Enabled = true;
        }
        catch(Exception ex)
        {
            throw ex;
        }
        
    }

    protected void GetDataDisplay()
    {
        int sID = Convert.ToInt32(ddlSurvey.SelectedValue.ToString());
        int pcID=0;
        if (ddlSurvey.SelectedItem.Text.ToLower().EndsWith("l1"))
        {
            if (Convert.ToInt32(ddlPC.SelectedIndex) == 0)
            {
                pcID = 0;
                GetSurveyDetails(sID, pcID);
                gvReport.Visible = true;
                tblAvg.Visible = false;
                if (Session["organisationAverage"].ToString() != string.Empty)
                {
                    tblAvg.Visible = true;
                    lblPCAvg.Visible = true;
                    lblOrgAvg.Visible = true;
                    lblorgAvgScore.Visible = true;
                    lblorgAvgScore.Text = Session["organisationAverage"].ToString();
                }
            }
            else
            {
                pcID = Convert.ToInt32(ddlPC.SelectedValue.ToString());
                GetSurveyDetails(sID, pcID);
                gvReport.Visible = true;
                tblAvg.Visible = false;
            }
            ddlPC.Enabled = true;
            
        }
        else
        {
            ddlPC.Enabled = false;
        }
        

    }


  protected void btnExportExcel_Click(object sender, EventArgs e)
    {

        try
        {
            btnExportExcel.Enabled = false;
            ExportToExcel(ExportExcel, ddlPC.SelectedItem.Text.ToString());
            //ExportExcel.Visible = true;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

    private void ExportToExcel(HtmlGenericControl ExportExcel, string strFileName)
    {
        
            //ExportExcel.Visible = true;
            GetDataDisplay();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + strFileName + ".xls");
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            ExportExcel.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
    }

推荐答案

您可以使用自定义控件来将gridview导出到excel.
隐藏具有按钮的列
在您使用的导出按钮单击事件上添加此代码
You can use this custom control to export gridview to excel.
hide that column that have button
add this code on export button click event that you have used
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
    GridView1.Columns[0].Visible = false;//first column hide
else
{
    GridView1.HeaderRow.Cells[0].Visible = false;
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        gvr.Cells[0].Visible = false;
    }
}



要将网格视图导出到excel文件,请使用以下代码

Hi,
For Exporting grid view to excel file use following code

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=g.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter ht = new HtmlTextWriter(sw);
gvDisplay.AllowPaging = false;
gvDisplay.DataBind();
gvDisplay.RenderControl(ht);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End()



并在aspx.cs页面中添加以下方法



and just add following metod in aspx.cs page

public override void VerifyRenderingInServerForm(Control control)
    {

    }


祝你好运
幸福的编码


Best Luck
happy Coding


我们可以添加两个函数.
一个用于显示控件的控件:可以在导出excel之前使用
和其他用于隐藏控件的控件:可以在导出excel后使用
We can add two functions.
one for displaying the controls: this can be used before export excel
and other for hiding the controls: this can be used after export excel


这篇关于在asp.net中将Gridview数据导出到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆