Pages

Men

rh

5/28/2013

How to save the Export to Excel file from Gridview to Specific Folder in C#

SqlConnection con = new SqlConnection();

con.Open();


SqlCommand cmd = new SqlCommand();


cmd.Connection = con;


cmd.CommandType = CommandType.StoredProcedure;


cmd.CommandText = "SP_GelAllEmaployeeDetail";


SqlDataAdapter da = new SqlDataAdapter(cmd);


DataSet ds = new DataSet();


da.Fill(ds);


gdvReport1A.DataSource = ds;

gdvReport1A.DataBind(); 


In Web.conf file i have folder name. Please find the settings of Web.Config file.

<add key="FolderName" value="TestFolder" />

 strFolderName = System.Configuration.ConfigurationManager.AppSettings["FolderName"].ToString();

 strFolderPath = strDirectoryPath + @"\" + strFolderName;


 DirectoryInfo dr = new DirectoryInfo(strFolderPath);


 if (dr.Exists == false)
 {
      dr.Create();
 }


 System.IO.StringWriter stringWrite = new System.IO.StringWriter();

 System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


 gdvReport1A.RenderControl(htmlWrite);


 String renderedGridView = stringWrite.ToString();


 File.WriteAllText(@"C:\Royalty\Report1A.xls", renderedGridView);



No comments :

Post a Comment