grid view to Excel without user selection to save with static path

Posted by Sateeshkumar.Singupuram under C# on 6/17/2013 | Points: 10 | Views : 1020 | Status : [Member] | Replies : 2
hi i am working on web application, and i need to export some data from grid view to Excel . which i am able to do propely with Http Responce but need some help not to give any user selection while saving excel but with a static File path. is there a way to do so? here is my as follows

Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName + ".xls"));
Response.Charset = "";
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvPreview.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

which will ask for user input to select a location to save the Exported .xls file.

sateesh kumar singupuram


Responses

Posted by: Chaithragm on: 6/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hope this will help you..
protected void excel_Click_btn(object sender, EventArgs e)
{
string Str = "SELECT * from table ";
SqlConnection con = con_manager.getcon();
SqlCommand cmd = new SqlCommand(Str, con);
SqlDataAdapter sda = new SqlDataAdapter(Str, con);
sda.Fill(dt);
GenerateTableData(dt);


}
public void GenerateTableData(DataTable dtdata)
{
string name = "Report" + System.DateTime.Now.ToString("ddMMMMyyyy") + ".xls";
string attach = "attachment;filename=" + name;
Response.ClearContent();
Response.AddHeader("content-disposition", attach);
Response.ContentType = "application/ms-excel";
if (dtdata != null)
{
foreach (DataColumn dc in dtdata.Columns)
{
Response.Write(dc.ColumnName + "\t");
//sep = ";";
}
Response.Write(System.Environment.NewLine);
foreach (DataRow dr in dtdata.Rows)
{
for (int i = 0; i < dtdata.Columns.Count; i++)
{
Response.Write(dr[i].ToString() + "\t");
}
Response.Write("\n");
}
Response.End();
}


}


Sateeshkumar.Singupuram, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sateeshkumar.Singupuram on: 6/17/2013 [Member] Starter | Points: 25

Up
0
Down
hi chaithra,
thanks for your reply but it is not solving my issue as i am able to save the excel file properly where ever i need to do so and unable to retrieve the path where i saved that file. As i need to copy another file at the same moment without prompting the user to select another location. i will be saving two files with a single selection of the location.

Regards,
Sateesh

sateesh kumar singupuram

Sateeshkumar.Singupuram, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response