Monday, April 25, 2011

export data from grid to excel

In aspx file(Design view)

<table cellpadding="0px" cellspacing="0px" width="750px">
        <tr>
            <td>
                <h3 style="border-bottom: 2px ridge black;">
                    Detail Report</h3>
            </td>
        </tr>
        <tr>
            <td align="right">
                <asp:Button ID="cmdExport" runat="server" Text="Export" OnClick="cmdExport_Click" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server" Width="750px" CellPadding="4" ForeColor="#333333"
                    GridLines="Vertical" AutoGenerateColumns="false">
                    <RowStyle BackColor="#EFF3FB" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:BoundField HeaderText="Reg.No." DataField="id" />
                        <asp:TemplateField HeaderText="ThumbImage">
                            <ItemTemplate>
                                <img src='user/profile_images/<%#Eval("ThumbImage") %>' width="50px" height="50px" alt="" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField HeaderText="Name" DataField="name" />
          
                        <asp:BoundField HeaderText="Address" DataField="address" />
                        <asp:BoundField HeaderText="EmailID" DataField="email_id" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>


In aspx.cs


    protected void Page_Load(object sender, EventArgs e)
    {
SqlConnection conn = new SqlConnection("server=.;database=test;uid=test;pwd=test");
        if (!Page.IsPostBack)
        {
            gridBind();
        }
    }

    void gridBind()
    {
        string sqlquery = "select * from table_name";
        DataSet ds = new DataSet();
        try
        {
            SqlDataAdapter sda = new SqlDataAdapter(sqlquery , conn);
sda.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
        catch
        {
        }

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        gridBind();
    }
    protected void cmdExport_Click(object sender, EventArgs e)
    {
        // Let's hide all unwanted stuffing
        this.GridView1.AllowPaging = false;
        this.GridView1.AllowSorting = false;
        this.GridView1.EditIndex = -1;

        // Let's bind data to GridView
        this.gridBind();
        // Let's output HTML of GridView
        Response.Clear();
        Response.ContentType = "application/vnd.xls";
        Response.AddHeader("content-disposition",
                "attachment;filename=report.xls");

        StringWriter swriter = new StringWriter();
        HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

        HtmlForm frm = new HtmlForm();
        this.GridView1.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(this.GridView1);
        frm.RenderControl(hwriter);

        Response.Write(swriter.ToString());
        Response.End();
    }

Thursday, April 7, 2011

Making a watermark on any image

 ///Locate Image from Image folder.
        System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("photo.jpg"));
        //Taken Actual width anf height From Image
        int height = objImage.Height;//height
        int width = objImage.Width;//Width
        //Create a Bitmap Image
        System.Drawing.Bitmap bitmapimage = new System.Drawing.Bitmap(objImage, width, height);// create bitmap with same size of Actual image
        //Convert in to a Graphics object
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmapimage);
        //Creating Brushe
        System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255));
        g.RotateTransform(-90);
        //g.TranslateTransform(150, 0);
        g.DrawString("Astromilan.com", new Font("Arial", 18, System.Drawing.FontStyle.Bold), brush, -230, 50);
        Response.ContentType = "image/jpeg";//setting ContentType
        bitmapimage.Save(Response.OutputStream, ImageFormat.Jpeg);//save image with dynamic watermark

Total Pageviews