Monday, 28 April 2014

Exporting the gridview data to excel sheet;


Create a grid view and bind the data to grid view now suppose your grid view is looking like below image;



Code for grid view


<tr>
<td style="height: auto;padding-left:5px" colspan="3">&nbsp;
     <asp:ImageButton runat="server" id="ImgExporttoExcel" Visible="false"  ImageUrl="~/Images/save.png" OnClick="ImgExporttoExcel_Click" />
 </td>
</tr>
<tr>
<td style="height: auto;padding-left:5px" colspan="3">
<asp:GridView ID="gdvSearchResult" AllowSorting="true"                                             AllowPaging="true" PageSize="25" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GdvSearchResult_PageIndexChanging">
 <Columns>
  <asp:BoundField HeaderText="Own Code#" DataField="OwnCode" SortExpression="OwnCode"
                                                    HeaderStyle-Width="6%" />
 <asp:TemplateField ShowHeader="true" HeaderText="Operation">
                                                                        <ItemStyle CssClass="Headercolor" HorizontalAlign="Center" Width="8%" />
                                                                        <HeaderStyle CssClass="Headercolor" HorizontalAlign="Center" Width="8%" />
                                                                        <ItemTemplate>
                                                                            <asp:Label ID="lblOperations" ForeColor="Black"  runat="server" Text='<%# GetOperation(Eval("Operation").ToString())%>'></asp:Label>
                                                                        </ItemTemplate>
                                                   </asp:TemplateField>
                                                <%--<asp:BoundField HeaderText="Operation" DataField="Operation" SortExpression="Operation"
                                                    HeaderStyle-Width="12%" />--%>
                                                 <asp:BoundField HeaderText="Sending Field" DataField="SendingField"
                                                    HeaderStyle-Width="14%" />
                                                 <asp:BoundField HeaderText="Recieving Field" DataField="RecievingField"  HeaderStyle-Width="6%" />
                                                <asp:BoundField HeaderText="Condition" DataField="Condition"
                                                    HeaderStyle-Width="12%" ItemStyle-Wrap="false" />
                                                <asp:BoundField HeaderText="Frequency" DataField="Frequency"
                                                    HeaderStyle-Width="6%" />
                                              
                                            </Columns>
                                        </asp:GridView>
                                    </td>
                                </tr>

                            </table>
Now click on the save button on top of the grid; it will call the method to export the grid data to excel sheet;
On Image click event call the methods as like below;
protected void ImgExporttoExcel_Click(object sender, ImageClickEventArgs e)
        {
            ExportGridData();
        }

Now event with call the export to Grid Data method where the actual code of export to excel are written;

public void ExportGridData()
        {

            string attachment = "attachment; filename=Emp.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gdvSearchResult.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

Along with above code place the below code also into the form as grid view render control throw error about form server control  not defined as runat=”server”; it will occur only if you are using master page;

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

Once you exported successfully; you are getting the excel sheet records like below;





No comments:

Post a Comment