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

我写了一个程序,其中的要求是当用户select特定的date,然后应显示一个报告。 在这里,我所展示的报告是在GridView的单独页面下button点击甚至。 但现在需要下载报告,同时他们点击查看并存储到某个特定的文件夹中。 所以,我写了下面的代码,它正在将报告下载到excel格式到特定的文件夹,但我不能再看到gridview。 在将数据绑定到gridview之后,我将下面的函数放到了pageload中,但是现在我可以看到,在debugging指向绑定网格的指针并一次又一次导出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); } } 

提前致谢。

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. 你有你的主要button“btnsubmit”,点击其中,gridview将被填充。
  2. 创build一个隐藏的button“btnexport”,将其样式设置为不显示,以便用户不可见。 在其单击事件中,编写代码以导出excel。
  3. 在你的aspx页面上定义一个函数“ExportExcel(){}”,它将dynamic调用隐藏button的click事件。
  4. 当用户点击“btnsubmit”时,在服务器端,代码绑定Gridview并使用“ScriptManager.RegisterStartupScript”执行“ExportExcel”
  5. ExportExcel,inturn,执行带有代码的“btnexport”的点击事件,将excel导出到浏览器。
  6. 不要忘记有jquery的参考。

你需要改变这样做的方式。

  • 创build一个具有Gridview的页面来显示数据,并添加一个控件(ActionLink或者Button)来导出数据到它旁边的excel。
  • 现在使用默认视图来显示GridView1的网格
  • 把你的代码负责在另一个方法中生成Excel,并在button或ActionLink上单击调用该方法。并使用GridView2生成Excel,而不是GridView1。