i am exporting gridview data to excel i am not able to export and i am not receiving any error
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None" AllowSorting="true" CellPadding="0" CssClass="table" GridLines="Both" Width="100%" OnRowCommand="GridView1_RowCommand">
<AlternatingRowStyle CssClass="odd" />
<Columns>
<asp:BoundField DataField="Department" HeaderText="مدیریت عمومی" SortExpression="Department" />
<asp:BoundField DataField="GDepName" HeaderText="امریت" SortExpression="GDepName" />
<asp:BoundField DataField="DName" HeaderText="ریاست" SortExpression="DName" />
<asp:BoundField DataField="GDirectorateName" HeaderText="ریاست عمومی" SortExpression="GDirectorateName" />
<asp:BoundField DataField="Moeenyat" HeaderText="معنیت" SortExpression="Moeenyat" />
<asp:BoundField DataField="Comments" HeaderText="ملاحظات" SortExpression="Comments" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lbEdit" Style="text-decoration: none;" runat="server" CommandName="lbEdit" CommandArgument='<%# Eval("Id") %>'>
<asp:Image ID="imgEdit" runat="server" ImageUrl="~/images/edit.gif" ImageAlign="AbsMiddle" />
تغیر </asp:LinkButton>
|
<asp:LinkButton ID="lbDelete" Style="text-decoration: none;" runat="server" CommandName="lbDelete" CommandArgument='<%# Eval("Id") %>' OnClientClick='if(!confirm("ایا میخواهد این ریکارد را حذف کند؟")){ return false; };'>
<asp:Image ID="ImgDelete" runat="server" ImageUrl="~/images/del.gif" ImageAlign="AbsMiddle" />
حذف </asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="110px" />
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pagging" />
</asp:GridView>
private void ExportGridToExcel()
{
//Response.ClearContent();
//Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
//Response.ContentType = "application/vnd.ms-excel";
//System.IO.StringWriter stringWriter = new StringWriter();
//HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
//GridView1.RenderControl(htmlTextWriter);
//Response.Write(stringWriter.ToString());
//Response.End();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
GridView1.AllowPaging = false;
FillGridView();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Forms/Main.master" AutoEventWireup="true" EnableEventValidation ="false" CodeFile="DepartmentForm.aspx.cs" Inherits="Forms_DepartmentForm" %>
please help me