Friday, 5 July 2013

Simple Date Report and Export Gridview to PDF

Simple Date Report and Export Gridview to PDF
For PDF you have to Downalod
For exporting the data, I am using the iTextSharp (third party dll) in this post. Download the iTextSharp .
iTextSharp .DLL files and Add in your Project
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Report.aspx.cs" Inherits="Report" %>

<!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>
   <table class="style1" align="center">
                <tr>
                    <td bgcolor="#99CCFF" colspan="2" style="text-align: center">
                        S<b>how Reports on User Registered </b></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblstartdate" runat="server" Text="Registration Date"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtstartingdate" runat="server"></asp:TextBox>
                        <br />
                        <asp:Calendar ID="Calendar1" runat="server"
                            onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
                    </td>
                </tr>
                <%--<tr>
                    <td>
                        <asp:Label ID="lblendingdate" runat="server" Text="Ending Date"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtendingdate" runat="server"></asp:TextBox>
                    </td>
                </tr>--%>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                        <asp:Button ID="btnfilter" runat="server" Text="Show  Registered Users" Width="166px"
                            onclick="btnfilter_Click" />
                    </td>
                </tr>

                <tr>
                <td><asp:ImageButton ID="btnPDF" runat="server" ImageUrl="~/Images/pdf.jpg"   Width="32px"
Height="32px" onclick="btnPDF_Click"/></td>
                </tr>


                <tr>
                <td><asp:GridView ID="GVMec" runat="server">
    </asp:GridView></td>
                </tr>
            </table>
</div>

   

    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;
using System.Data;


using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

using System.IO;

public partial class Report : System.Web.UI.Page
{
    string Str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnfilter_Click(object sender, EventArgs e)
    {
       
          SqlConnection conn= new SqlConnection(Str);
          
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                // cmd.Parameters.Add(new SqlParameter("@Date1", SqlDbType.DateTime)).Value = txtstartingdate ;
                SqlDataAdapter sda = new SqlDataAdapter("Select * From Tbl_User_Registration Where U_Rdate= '" + txtstartingdate.Text + "' ", conn);
                SqlCommandBuilder scb = new SqlCommandBuilder(sda);
                //Create a DataTable to hold the query results.
                DataTable dTable = new DataTable();
                //Fill the DataTable.
                sda.Fill(dTable);
                if (dTable.Rows.Count > 0)
                {
                    GVMec.DataSource = dTable;
                    GVMec.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('No Recodrd Found')</script>");
                }
  

               
                
        
                   
               }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        txtstartingdate.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
      
    }





// Below is Code for Exporting Gridview to PDF
    protected void btnPDF_Click(object sender, ImageClickEventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition""attachment;filename=Sample.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        //Set AllowPaginf false to export the full data
        GVMec.AllowPaging = false;
       // GVMec.DataBind();
        //Start the rendering of control here
        GVMec.RenderBeginTag(hw);
        GVMec.HeaderRow.RenderControl(hw);
        foreach (GridViewRow row in GVMec.Rows)
        {
            row.RenderControl(hw);
        }
        GVMec.FooterRow.RenderControl(hw);
        GVMec.RenderEndTag(hw);
        //Apply some style settimgs
        GVMec.Caption = "Your caption";
        GVMec.Style.Add("width""400px");
        GVMec.HeaderRow.Style.Add("font-size""12px");
        GVMec.HeaderRow.Style.Add("font-weight""bold");
        GVMec.Style.Add("border""1px solid black");
        GVMec.Style.Add("text-decoration""none");
        GVMec.Style.Add("font-family""Arial, Helvetica, sans-serif;");
        GVMec.Style.Add("font-size""8px");
        StringReader sr = new StringReader(sw.ToString());
        //creating new pdf document
        Document pdfDoc = new Document(PageSize.A4, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
        Response.Clear();

       

      
    }

  
}

No comments:

Post a Comment