Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why error is giving int the line gvdetails.RenderControl(htw)? AND tell gvdetails of GridView must be inside of Form tag with runnat = Server

HTML CODE GIVEN BELOW
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Export Data from Grid to Word and Excel.aspx.cs" Inherits="Export_Data_from_Grid_to_Word_and_Excel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <div align="center">
    
        <br />
        <br />
        <asp:ImageButton ID="btWord" runat="server" Width="98px" 
            ImageUrl="~/word_icon[1].png" Height="93px" onclick="ImageButton1_Click" />
     
        <asp:ImageButton ID="btExcel" runat="server" Width="119px" 
            ImageUrl="~/excel[1].gif" Height="108px" onclick="ImageButton2_Click" />
        <asp:GridView ID="gvdetails" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" DataKeyNames="qid" 
            DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#E7E7FF" 
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" 
            Width="260px">
            <AlternatingRowStyle BackColor="#F7F7F7" />
            <Columns>
                <asp:BoundField DataField="qid" HeaderText="qid" SortExpression="qid" 
                    ReadOnly="True" />
                <asp:BoundField DataField="qname" HeaderText="qname" 
                    SortExpression="qname" />
                <asp:BoundField DataField="sid" HeaderText="sid" SortExpression="sid" />
                <asp:BoundField DataField="aid" HeaderText="aid" SortExpression="aid" />
                <asp:BoundField DataField="cid" HeaderText="cid" SortExpression="cid" />
                <asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />
            </Columns>
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <SortedAscendingCellStyle BackColor="#F4F4FD" />
            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
            <SortedDescendingCellStyle BackColor="#D8D8F0" />
            <SortedDescendingHeaderStyle BackColor="#3E3277" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:tuitionConnectionString %>" 
            SelectCommand="SELECT * FROM [question]"></asp:SqlDataSource>
        <br />
    
    </div>
    </form>
</body>
</html>

SEE MY CS CODE IS GIVEN BELOW FOR THIS
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Export_Data_from_Grid_to_Word_and_Excel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        gvdetails.AllowPaging = false;

        gvdetails.DataBind();

        Response.ClearContent();

        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.doc"));

        Response.Charset = "";

        Response.ContentType = "application/ms-word";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        gvdetails.RenderControl(htw);

        Response.Write(sw.ToString());

        Response.End();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.ClearContent();

Response.Buffer = true;

Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));

Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

gvdetails.AllowPaging = false;

gvdetails.DataBind();

gvdetails.HeaderRow.Style.Add("background-color", "#FFFFFF");

for (int i = 0; i < gvdetails.HeaderRow.Cells.Count; i++)

{

gvdetails.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");

}

int j = 1;

foreach (GridViewRow gvrow in gvdetails.Rows)

{

gvrow.BackColor = Color.White;

if (j <= gvdetails.Rows.Count)

{

if (j % 2 != 0)

{

for (int k = 0; k < gvrow.Cells.Count; k++)

{

gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");

}

}

}

j++;

}

gvdetails.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();
    }
}
Posted
Updated 30-Dec-11 23:28pm
v2
Comments
OriginalGriff 31-Dec-11 3:54am    
Don't start a new question to give better information, and format your answer correctly, useing code blocks - I did it for you last time!
Instead, use the "Improve question" widget to edit your question and provide better information.

I will delete the original question.
OriginalGriff 31-Dec-11 3:56am    
On second thoughts, I won't - so you can see what the difference is between your code formatted and unformatted...

1 solution

Use the code below,

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
   {
       ConvertXL(Response);
   }
   public void ConvertXL(HttpResponse Response)
   {
       try
       {
           gvdetails.SelectedIndex = -1;
           Response.Clear();
           HtmlForm form = new HtmlForm();
           string attachment = "attachment;filename=UserLogDetails.xls";
           Response.ClearContent();
           Response.AddHeader("content-disposition", attachment);
           Response.ContentType = "application/vnd.xls";
           StringWriter stw = new StringWriter();
           HtmlTextWriter htextw = new HtmlTextWriter(stw);
           form.Controls.Add(gvdetails);
           this.Controls.Add(form);
           form.RenderControl(htextw);
           Response.Write(stw.ToString());
           Response.End();
       }
       catch (Exception ex)
       {
           return;
       }
   }



And for export to Word use

Response.ContentType = "application/ms-word";
 
Share this answer
 
v4
Comments
Janardan Pandey 31-Dec-11 5:03am    
But the grid view data is not showing in ms word file which is opened
Supriya Srivastav 31-Dec-11 5:03am    
plz. check the updated code.
Supriya Srivastav 31-Dec-11 5:05am    
import namespace "using System.Web.UI.HtmlControls" if not imported yet.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900