Tuesday, April 6, 2010

Down load file from Solution Explorar

protected void Button2_Click(object sender, EventArgs e)
    {
        string s = DropDownList1.SelectedItem.Text;
        Response.Clear();
        Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", s));
        Response.Charset = "";
        Response.ContentType = "application/vnd.xls";

        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    
        Response.Write(stringWrite.ToString());
        Response.End();
    }


Retrive the files For Dropdownlist


 protected void Page_Load(object sender, EventArgs e)
    {
        retrivefiles();
    }
    public void retrivefiles()
    {
      
        DirectoryInfo dinfo = new DirectoryInfo(Server.MapPath(("." + @"\images\")));
        FileInfo[] fileslist = dinfo.GetFiles();
        foreach (FileInfo fi in fileslist)
        {
            DropDownList1.Items.Add(fi.Name);
        }
    }

export data from gridview to excel And Pdf

Export To Excel
protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.xls");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView gv = new GridView();
        gv.DataSource = ds;
        gv.DataBind();

     gv.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }


export to Pdf


 protected void Button2_Click(object sender, EventArgs e)
    {

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=DocumentReport.pdf");
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/pdf";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView gv = new GridView();
        gv.DataSource = ds;
        gv.DataBind();
       gv.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }

Retrive the data from excel sheet ,Xml And Sql server2005

 protected void Button1_Click(object sender, EventArgs e)
    {
        sqlCon.Open();
        SqlCommand cmd = new SqlCommand("select type from spreadsheet where spreadsheetname='" + DropDownList1.SelectedItem.Text + "'", sqlCon);
        SqlDataReader dr = cmd.ExecuteReader();
        string type="";
        if (dr.Read())
        {
            type = dr[0].ToString();
        }
        sqlCon.Close();
        string filename = DropDownList1.SelectedItem.Text;
        switch (type)
        {
            case "Excel":
               
                string conn = ("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath(".\\datasources\\" + filename) + ";Extended Properties=\"Excel 8.0;\""); // or use instead of Excel 8.0 - Excel 5.0
                string SSQL = "SELECT *  from [sheet1$]";
                OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn); // here use oleDataReader
                DataSet ds = new DataSet();
                //oleDA.TableMappings.Add("Table","ExcelTest"); // Require
                oleDA.Fill(ds);
                GridView1.DataSource = ds.Tables[0].DefaultView; // or [ ds ]
                GridView1.DataBind();
                break;
            case "XML":
                DataSet ds1 = new DataSet();
                ds1.ReadXml(Server.MapPath(@".\datasources\" + filename));
                GridView1.DataSource = ds1.Tables[0];
                GridView1.DataBind();
                break;
            case "SQLSERVER":
                SqlCommand cmd1 = new SqlCommand("select * from "+filename,sqlCon);
                SqlDataAdapter da = new SqlDataAdapter();
                DataSet ds2 = new DataSet();
                da.Fill(ds2);
                GridView1.DataSource= ds2.Tables[0];
                GridView1.DataBind();
                break;
        }




Get the All filenames in to dropdownlist from solution Explorar

 SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string username = (string)Session["username"];
            sqlCon.Open();
            SqlCommand cmd = new SqlCommand("select spreadsheetname from spreadsheet where username='"+username+"'", sqlCon);
            SqlDataReader dr = cmd.ExecuteReader();
            DropDownList1.Items.Add("Select");
            while (dr.Read())
            {
                DropDownList1.Items.Add(dr[0].ToString());
            }
            sqlCon.Close();
        }
    }
   
    }