Sunday, May 5, 2013

Export gridview data to word document in

Title:How to export data from grid view to word document in using c#

As per previous articles we have learnt the reporting is the mandatory step for any web application.The data can be exported in different formats like PDF,Excel etc,Now i would like to explain on export data to word document from grid view

What we have read :
how to export grid view to excel,bind excel data to grid view in this example will describe how to export Grid view data to word document in

<html xmlns="">
<form id="form1" runat="server">
<asp:GridView ID="gvOrder" AllowSorting="True" AllowPaging="True" Runat="server" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True"
AutoGenerateColumns="False" DataSourceID="OrdrDb">
<asp:BoundField DataField="OrderID" HeaderText="OrderID"/>
<asp:BoundField DataField="OrderName" HeaderText="OrderName"/>
<asp:BoundField DataField="Phone" HeaderText="Phone"/>
<asp:BoundField DataField="Address" HeaderText="Address"/>
<asp:BoundField DataField="Amount" HeaderText="Amount"/>
<asp:SqlDataSource ID="OrdrDb" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT * FROM [Orders]"></asp:SqlDataSource>
<asp:Button ID="btnexportOrderstoWord" runat="server" Text="Button"
onclick="btnexportOrderstoWord_Click" />

Code behind:
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class ExporttoWord : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnexportOrderstoWord_Click(object sender, EventArgs e)
Response.Buffer = true;
Response.ContentType = "application/ ";
StringWriter swr = new StringWriter();
HtmlTextWriter hwr = new HtmlTextWriter(swr);
gvOrder.AllowPaging = false;
public override void VerifyRenderingInServerForm(Control GvOrder)
Note:While executing this class we may get this kind of error because of render control.please click here for solution "RegisterForEventValidation can only be called during Render"
Post a Comment