Problem in Exporting Data from grid view to Excel file in asp.net

Posted by Surajemo under ASP.NET on 11/28/2012 | Points: 10 | Views : 2557 | Status : [Member] | Replies : 3
Hi............

I am trying to export grid view data to excel file.I have done this successfully but the problem is Excel file is storing in solution itself.but i want to save that excel file in downloads folder.how can i solve this? Please help me anyone....

here is my code


protected void btnExport_Click(object sender, EventArgs e)
{

DataTable dt = new DataTable();
dt = Session["data"] as DataTable;

if (dt == null)
{
throw new Exception("No Records to Export");
}
string Path = "D:\\ImportExcelFromDatabase\\Karvyexcel_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";

//string Path = "D:\\Sujatha\\Karvy_27Nov2012\\Inventory_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xls";
FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt;
DataGrd.DataBind();

DataGrd.RenderControl(htmlWrite);
////string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
////if (!Directory.Exists(directory))
////{
//// Directory.CreateDirectory(directory);
////}

//System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
stringWriter.ToString().Normalize();
//vw.Write(stringWriter.ToString());
//vw.Flush();
//vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());


}
public static void WriteAttachment(string FileName, string FileType, string content)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();

}

private void BindCustomerData()
{
try
{
string strSelect = "select * from CustomerData";
DataSet ds = new DataSet();
cnn.ConnectionString = connStr;
// if the connection will be closed the below code poen the connection when the page will be loaded.
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
// here i am using the store procedure named tb_gallery_insert to insert the record inside the database.
SqlDataAdapter da = new SqlDataAdapter(strSelect, cnn);
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
gvData.DataSource = ds.Tables[0];
gvData.DataBind();

Session["data"] = ds.Tables[0];

}
else
{
lblMessage.Text = "No Records Found";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
lblMessage.ForeColor = System.Drawing.Color.Red;
}

}




Responses

Posted by: Krv on: 11/28/2012 [Member] Starter | Points: 25

Up
0
Down
Give the Relative path to the 'path' variable (Server.MapPath("~/download/sample.xls")) and check it out

Thanks,
R.Vasanth
MCTS(SQL Server & BI)
http://bigdatatech.blogspot.com

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

Posted by: Surajemo on: 11/29/2012 [Member] Starter | Points: 25

Up
0
Down
thanks for the solution my friend
i just figured it out
it was due to update pannel

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

Posted by: Top10idea on: 11/30/2012 [Member] Starter | Points: 25

Up
0
Down
Visit Following link

http://code.top10idea.com/2012/11/30/exporting-data-from-grid-view-to-excel-file-in-asp-net/

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

Login to post response