Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi all,

am developing windows application using c#.net ,

in this i have one part to click download and display file in excell sheet,

in this portion i have done in asp.net using c# but i need only windows application

i tryied my below asp.net code in to windows application but am couldnt get any similar code,

so please find out my below asp.net code and give me a windows application code,

its very appreciable for help....



my asp.net code is:



C#
protected void download_Click(object sender, EventArgs e)
    {
        
        if (DDLCarrier.SelectedItem.Value == "DMSU")
        {
            SqlConnection con = new SqlConnection(ConnectionStr);
            SqlCommand cmd = new SqlCommand("datadownload", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Destinations", SqlDbType.VarChar);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            con.Close();

            DataTable dt = Getdata(cmd);

            GridView gv1 = new GridView();
            gv1.AllowPaging = false;
            gv1.DataSource = dt;
            gv1.DataBind();

            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);

            for (int i = 0; i < gv1.Rows.Count; i++)
            {
                gv1.Rows[i].Attributes.Add("class", "textmode");

            }
            gv1.RenderControl(hw);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
}


private DataTable Getdata(SqlCommand cmd)
    {
        DataTable dt = new DataTable();

        SqlConnection con = new SqlConnection(ConnectionStr);
        SqlDataAdapter da = new SqlDataAdapter();

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = con;
        try
        {
            con.Open();
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            da.Dispose();
            con.Dispose();

        }
    }
Posted
Updated 27-Feb-13 0:14am
v2
Comments
boogac 27-Feb-13 6:14am    
Convert.ToWindowsApplication(ASP.Net) in the future maybe ?
Balamurugan1989 27-Feb-13 7:58am    
In Windows Form application we can use Download option to download the Excel File.
Balamurugan1989 27-Feb-13 8:01am    
Have you got the solution or could i post it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900