Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi. I am working on a web application, and I need to export some data from gridview to Excel. Which I am able to do propely with HttpResponse 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 code:
C#
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();

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

sateesh kumar singupuram));
C#
Response.Charset = 
Posted
Updated 16-Jun-13 23:18pm
v2
Comments
Mahesh Bailwal 17-Jun-13 5:26am    
So you want to display a popup with text-box where user can enter the location for saving export data?
Member 10026544 17-Jun-13 6:15am    
hi Mahesh, not exactly i am able to save my file where ever i am interested, but at the same time i am not able to render the same path where i am saving the first file. for example i am saving an excel file from gridview by using HTTP Responce class as i said earlier. so the same path where i have saved the excel file need to be used to save another file immediately instead of selecting same path again through the UI. so how can i get the first saved path from the browser. here is my Export Excel code


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

1 solution

No, it's not possible. You cannot assign a file destination on the client computer(Due to security issue). Here in your code(or all download wizard), server will throw the file to browser, which will cause a prompt to open/download the file. It's up to the client to save/discard the file.

--Amit
 
Share this answer
 
Comments
Member 10026544 17-Jun-13 6:01am    
hi amit, thanks for the immediate reply but i am stuck with an issue based on this scenario. i need to get the path of the user selection after export. For example if the user selected a folder from c:\ drive then i need to get that path so that i can save one more file along with the exported excel file. so is there any way to retrieve the user selected file path? please help me out with this.
_Amy 17-Jun-13 6:07am    
The below link may help you:
Windows RegKey - Default Browser Application Path[^]
--Amit

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