Tuesday, 1 April 2014

Export GridView Data with Image in C#.net

Write this code on .aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="Job" HeaderText="Job" />
                <asp:BoundField DataField="Location" HeaderText="Location" />
                <asp:ImageField DataImageUrlField="Image" HeaderText="Image">
                </asp:ImageField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Export" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>





copy & paste this code on .cs page....

protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            FillGridview();
        }
    }

    private void FillGridview()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Job", typeof(string));
        table.Columns.Add("Location", typeof(string));
        table.Columns.Add("Image");

        table.Rows.Add("JP", "XXX", "QQQQ", "http://localhost:10735/WebSite2/image/sub-menu-corner.png");
        table.Rows.Add("HP", "TTT", "AAAA", "http://localhost:10735/WebSite2/image/sub-menu-corner-2.png");
        table.Rows.Add("SQ", "YYY", "HHHH", "http://localhost:10735/WebSite2/image/section1-bg.jpg");
        table.Rows.Add("XS", "EEE", "UUUU", "http://localhost:10735/WebSite2/image/form-bg.png");
        GridView1.DataSource = table;
        ViewState["dt"] = table;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        DataTable dt = (DataTable)ViewState["dt"];
        GridView1.DataSource = dt;
        GridView1.DataBind();

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            //Apply text style to each Row
            row.Attributes.Add("class", "textmode");
        }
        GridView1.RenderControl(hw);
        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }


Result: