Showing posts with label Export. Show all posts
Showing posts with label Export. Show all posts

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:


Tuesday, 8 October 2013

ASP.NET - Export Gridview data to excel and directly send a mail with exported excel file

On Control Event use this code in C#:

                        GridView gdvExportxls = new GridView();
                        gdvExportxls.DataSource = dtnew;
                        gdvExportxls.DataBind();
                        MemoryStream ms = new MemoryStream();
                        System.IO.StringWriter stringWriter = new System.IO.StringWriter();
                        System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
                        gdvExportxls.RenderControl(htmlWriter);
                        byte[] excelFile = System.Text.Encoding.ASCII.GetBytes(stringWriter.ToString());
                        ms.Write(excelFile, 0, excelFile.Length);
                        ms.Position = 0;

                        Attachment attachment = new Attachment(ms, "Test.xls", "application/vnd.xls");
                        SendMail(attachment);


          private void SendMail(Attachment excel)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("brijesh.singh879@gmail.com")
            mail.Subject = "Export GridView to Excel";
            mail.Body = "mail with attachment";

            //System.Net.Mail.Attachment attachment;
            //attachment = excel;
            mail.Attachments.Add(excel);

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("brijesh.singh879@gmail.com", "*******");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

        }