在 asp.net c# 中导出到 Excel 后如何显示 Gridview?

How to show Gridview after exporting to Excel in asp.net c#?

我编写了一个程序,要求在用户 select 特定日期时显示一份报告。在这里,我显示的报告位于 Gridview 中的单独页面中,甚至在按钮单击下。但是现在需要在他们点击查看的同时下载报告并将其存储到某个特定的文件夹中。因此,我编写了以下代码,它正在努力将报告下载为 excel 格式到特定文件夹中,但我再也看不到 gridview。在将数据与 gridview 绑定后,我已将以下函数放入页面加载中,但现在我可以看到,在调试指向绑定网格的指针并一次又一次地导出 excel 时,页面显示错误“网页无法被展示。”请帮我解决这个问题。

这是我写入 excel 并存储到一个位置的代码示例:

     private void ExportPagetoExl()
{
    Response.Clear();
    Response.Buffer = true;
    if (string.IsNullOrEmpty(cpIdStr))
        Response.AddHeader("content-disposition", "attachment;filename="+ Request["year"] +"_" + Request["month"] + "_Transaction_" + requestChannel + ".xls");
    else
    {
        Response.AddHeader("content-disposition", "attachment;filename=" + Session["cpUsername"] + "_" + Request["year"] + "_" + Request["fullmonth"] + "_" + requestChannel + ".xls");
    }

    Response.Charset = string.Empty;
    Response.ContentType = "application/vnd.ms-excel";

    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    FileInfo FI = new FileInfo(@"C:\MyPC\Projects\report\transaction_" + requestChannel + "_" + Request["year"] + "_" + Request["fullmonth"] + ".xls");
    GridView1.AllowPaging = false;
    GridView1.DataSource = myData.DefaultView;
    GridView1.DataBind();


    try
    {
        GridViewRow header = GridView1.HeaderRow;
        GridView1.RenderControl(hw);
        string style = "<style>.textmode{mso-number-format:\@;}</style>";
        string directory = @"C:\MyPC\Projects\report\";
        if (!Directory.Exists(directory))
        {
            Directory.CreateDirectory(directory);
        }

        System.IO.StreamWriter vw = new System.IO.StreamWriter(@"C:\MyPC\Projects\report\" + Request["year"] + "_" + Request["month"] + "_Transaction_" + requestChannel + ".xls", true);
        sw.ToString().Normalize();
        vw.Write(sw.ToString());
        vw.Flush();
        vw.Close();
        Response.Flush();
        Response.BufferOutput = true;
        Response.Close();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

提前致谢。

你需要改变做事的方式。

  • 创建一个包含 Gridview 的页面以显示数据并添加一个控件(ActionLink 或 Button)以将数据导出到它旁边的 excel。
  • 现在使用默认视图显示网格,即 GridView1
  • 将负责生成 Excel 的代码放在另一个方法中,然后在按钮或 ActionLink 上调用该方法 click.And 使用 GridView2 生成 excel 而不是 GridView1。

ASPX代码

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript">
            function ExportExcel() { //function to be executed from server side after gridview is bound
                $('#btnexport').click();
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="btnsubmit" Text="Submit" runat="server" ClientIDMode="Static" OnClick="btnsubmit_Click" />
                <asp:Button ID="btnexport" runat="server" ClientIDMode="Static" style="display:none;" OnClick="btnexport_Click" /><!--Hidden button  -->
                <asp:GridView ID="Gridview1" runat="server"></asp:GridView>
            </div>
        </form>
    </body>
    </html>

代码隐藏

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        //Bind Gridview
        LoadGridview();
        //Call "ExportExcel();" Javascript function which has been defined in the aspx page 
        ScriptManager.RegisterStartupScript(this, this.GetType(), "com", "ExportExcel();", true);
    }

    protected void btnexport_Click(object sender, EventArgs e)
    {
        //export the file here
        ExportToExcel();
    }
    private void LoadGridview()
    {
        //Your code to bind gridview goes here
    }
    private void ExportToExcel()
    {
        //Your code to export goes here
    }

说明

  1. 您有主按钮 "btnsubmit",单击该按钮,将填充 gridview。
  2. 创建一个隐藏按钮 "btnexport",将其样式设置为显示 none 以便用户不可见。在其点击事件中,编写代码以导出 excel.
  3. 在您的 aspx 页面上定义一个函数 "ExportExcel(){}",它将动态调用隐藏按钮的点击事件。
  4. 当用户在服务器端单击 "btnsubmit" 时,代码绑定 Gridview 并使用 "ScriptManager.RegisterStartupScript"
  5. 执行 "ExportExcel"
  6. ExportExcel 依次执行 "btnexport" 的点击事件,其中包含将 excel 导出到浏览器的代码。
  7. 别忘了有 Jquery 参考。