Click here to Skip to main content
16,010,022 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Store datatable data on excel sheet and this excel sheet save on particular drive of my PC like on D:

i have to do following code but the excel sheet save on Download folder of my PC.

How i would to save it on D: ?

What I have tried:

protected void btndatatabletoexcel_Click(object sender, EventArgs e)
{
objtestBO.Id = 1;
objtestBO.Name = txtnm.Text;
objtestBO.City = txtcity.ToString();
int result = objtestBL.Insert_test(objtestBO);
if (result > 0)
{
u.MsgThenRedirectTo(objtestBO.Name + " Added Successfully!!", "datatabletoexcel");
DataTable dt = new DataTable();
dt = objtestBL.Selecttest();
UploadDataTableToExcel(dt);
}
}

protected void UploadDataTableToExcel(DataTable dtRecords)
{
string XlsPath = Server.MapPath(@"~/Add_data/test.xls");
string attachment = string.Empty;
if (XlsPath.IndexOf("\\") != -1)
{
string[] strFileName = XlsPath.Split(new char[] { '\\' });
attachment = "attachment; filename=" + strFileName[strFileName.Length - 1];
}
else
attachment = "attachment; filename=" + XlsPath;
try
{
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = string.Empty;

foreach (DataColumn datacol in dtRecords.Columns)
{
Response.Write(tab + datacol.ColumnName);
tab = "\t";
}
Response.Write("\n");

foreach (DataRow dr in dtRecords.Rows)
{
tab = "";
for (int j = 0; j < dtRecords.Columns.Count; j++)
{
Response.Write(tab + Convert.ToString(dr[j]));
tab = "\t";
}

Response.Write("\n");
}
Response.End();
}
catch (Exception ex)
{
//Response.Write(ex.Message);
}
}
Posted
Updated 1-Aug-16 0:17am

1 solution

You can't save files onto the client's hard drive, can you imagine the security issues if you could?

It is up to the user to decide where the file is saved, if it is saved at all. You can't control where they are saved to either.
 
Share this answer
 
Comments
Vibhusha Devani 1-Aug-16 6:23am    
My Requirement is,

i have to do this because it saves on server PC.Other PC which are connected with server PC by LAN.

the client should be fill the form on local PC and when click on save at that time generate the excel sheet of this form and this excel sheet save on server PC.

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