Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am trying to export a gridview to excel in a dynamic directory which gets created earlier in the code.

but as soon as the code for the export to excel is run it gives error is not a valid path.

i have tried several suggestions but all gives me the same error please see code below

C#
Response.AddHeader("content-disposition",
            "attachment;filename=Stockout_" + lblprevmnthname.Text + @"_" + lblyear.Text + ".xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.RenderControl(hw);
            string folderYear = lblyear.Text;
            string folderMonth = lblprevmnthname.Text;
            string serverDirectory = Server.MapPath("~/Monthly/" + folderYear + @"/" + folderMonth + @"/warehouse/" + "Stockout_" + lblprevmnthname.Text + @"_" + lblyear.Text + ".xls");
            var pathwarehouse = Server.MapPath(serverDirectory);
            File.WriteAllText(pathwarehouse, sw.ToString());
            GridView1.AllowPaging = false;
            GridView1.DataBind();
Posted

1 solution

Hi
Due to security reasons you cannot write a file from a server to the client .
You only make the file available in the browser to save or cancel .
 
Share this answer
 
Comments
Member 9742322 5-Feb-13 6:42am    
So i cant even save the file on the server itself? all that i am trying to do is render the file save and then email it out.

Can i ask what you suggest to accomplish this?
Vinodh.B 5-Feb-13 7:08am    
My suggestion is to make the file available in browser for download & save it the system & mail it .

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