Title:How to export data from grid view to word document in asp.net using c#
Description:
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
Example:
What we have read :
how to export grid view to excel,bind excel data to grid view in asp.net.Here this example will describe how to export Grid view data to word document in asp.net.
Description:
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
Example:
What we have read :
how to export grid view to excel,bind excel data to grid view in asp.net.Here this example will describe how to export Grid view data to word document in asp.net.
<html xmlns="http://www.w3.org/1999/xhtml"> <body> <form id="form1" runat="server"> <asp:GridView ID="gvOrder" AllowSorting="True" AllowPaging="True" Runat="server" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" AutoGenerateColumns="False" DataSourceID="OrdrDb"> <Columns> <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"/> </Columns> </asp:GridView> <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" /> </form> </body> </html>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.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=gvOrderToWord.doc"); Response.ContentType = "application/vnd.ms-word "; StringWriter swr = new StringWriter(); HtmlTextWriter hwr = new HtmlTextWriter(swr); gvOrder.AllowPaging = false; gvOrder.DataBind(); gvOrder.RenderControl(hwr); Response.Write(swr.ToString()); Response.Flush(); Response.End(); } 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"
No comments:
Post a Comment