GridView导出问题

时间:2014-10-27 08:07:11

标签: c# asp.net

在下面的代码中我有一个网格视图,它与我想要以excel格式导出数据的数据绑定。但是我无法导出请帮我解决问题。网格视图被放置内部更新面板。

 <asp:UpdateProgress ID="UpdateProgress2" AssociatedUpdatePanelID="HoardReportPanel" runat="server">
                <ProgressTemplate>
                    Details are loading... Please wait
                </ProgressTemplate>
            </asp:UpdateProgress>
             <asp:UpdatePanel ID="HoardReportPanel" runat="server">
                <ContentTemplate>
    <asp:GridView Width="100%" runat="server" ID="GridOpenstk" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333" ShowFooter="false" 
                        PageSize-Mode="NumericPages" PageSize="10" PagerStyle-Visible="true" BackColor="AliceBlue"  AllowPaging="true" AllowSorting="true"   
                        OnPageIndexChanging="GridOpenstk_PageIndex"
                        CssClass="mGrid"
                        PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" Visible="false" >
                        <Columns>                  

                        <asp:TemplateField HeaderText="Location" ItemStyle-Width="200px"   >              
                            <ItemTemplate>                                              
                                <asp:Label ID="lbllocation"  runat="server"  Text='<%#Eval("location") %>' > </asp:Label>
                            </ItemTemplate> 
                        </asp:TemplateField>
                         <asp:TemplateField HeaderText="ProductName" ItemStyle-Width="200px"  >              
                            <ItemTemplate>                                              
                                <asp:Label ID="lblProductName"  runat="server"  Text='<%#Eval("ProductName") %>' > </asp:Label>
                            </ItemTemplate> 
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="OpeningStock" itemstyle-width="150px">
                             <ItemTemplate>                                              
                                <asp:Label ID="lblOpeningStock"  runat="server"  Text='<%#Eval("OpeningStock") %>' > </asp:Label>
                            </ItemTemplate>     
                        </asp:TemplateField>  


                        </Columns>        
                        <HeaderStyle Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
                        <asp:Button ID="btnExportExcel" OnClick="btnExportExcel_Click" runat="server" Text="Export Excel"  />
    </ContentTemplate>
                </asp:UpdatePanel> 


     protected void btnExportExcel_Click(object sender, EventArgs e)
            {
                HtmlForm form = new HtmlForm();
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "Student.xls"));
                Response.ContentType = "application/ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                GridOpenstk.AllowPaging = false;
                string location = ddlLocation.SelectedValue;
                string startdate = Convert.ToDateTime(txtFromDate.Text + " 00:00:00").ToString("yyyy-MM-dd" + " 00:00:00");
                string enddate = Convert.ToDateTime(txtToDate.Text + " 23:59:59").ToString("yyyy-MM-dd" + " 00:00:00");
                MastersClient OpeningStock = new MastersClient();
                DataSet ds = OpeningStock.GetOpeningStockReport(location, startdate, enddate);
                GridOpenstk.DataSource = ds;
                GridOpenstk.DataBind();
                form.Attributes["runat"] = "server";
                form.Controls.Add(GridOpenstk);
                this.Controls.Add(form);
                form.RenderControl(hw);
                string style = @"<style> .textmode { mso-number-format:\@;}</style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

            public override void VerifyRenderingInServerForm(Control control)
            {
                /* Verifies that the control is rendered */
            }

1 个答案:

答案 0 :(得分:0)

如果GridView放在UpdatePanel内,您必须确保为该按钮创建了一个完整的PostBackTrigger

<asp:UpdatePanel ID="HoardReportPanel" runat="server">
    <ContentTemplate>
        .. grid ...
        <asp:Button ID="btnExportExcel" OnClick="btnExportExcel_Click" runat="server" Text="Export Excel"  />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnExportExcel" />
    </Triggers>
</asp:UpdatePanel>