Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello,
I spend lot of time on this problem, Can you solve it?

Problem is that I cant able to save file in application folder.
ok.
let know what I am doing here, I get data table from database and convert it into excel file. and this excel file I want to save in application folder.
below is my code...
public void exportToExcel(string userAutoId)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("getContacts", con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@userAutoId", userAutoId);
        //cmd.Parameters.Add("@retValue", System.Data.SqlDbType.VarChar).Direction = System.Data.ParameterDirection.ReturnValue; 
        da.Fill(dt);

        //Create a dummy GridView
        GridView GridView1 = new GridView();
        GridView1.AllowPaging = false;
        GridView1.DataSource = dt;
        GridView1.DataBind();

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition","attachment;filename=Contact_Details.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            //Apply text style to each Row
            GridView1.Rows[i].Attributes.Add("class", "textmode");
        }
        GridView1.RenderControl(hw);

        //style to format numbers to string
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        //Response.Write("{\"success\":true,\"isSuccess\":true,\"fileName\":\"\"}");
        Response.Write(style);
        Response.Output.Write(sw.ToString());

        //Here I want to code for save file in application folder


        Response.Flush();
        Response.End();
    }


What I have tried:

I am trying to save excel file in application folder.
Posted
Updated 19-Jan-17 18:35pm
Comments
[no name] 18-Jan-17 8:07am    
Hard to believe that you googled your "question". There must thousands of examples on the internet that demonstrate how to save files on your server.
ZurdoDev 18-Jan-17 9:35am    
This will give the user a download prompt in the browser. You cannot save to their specific folder.
ZurdoDev 18-Jan-17 9:35am    
Do you want to save in the server's folder? That's simple.
Vikas Hire 20-Jan-17 0:32am    
yes, I found the solution on it. Just two line code I insert in my existing code.

first line for get server's folder path that I want to save my file..
second line of code Creating the file name..
and third line of code save the file in folder.


string strPath = HostingEnvironment.ApplicationPhysicalPath + @"TempFiles/";
string fileName = Uid + "ContactList.xls";
File.WriteAllText(strPath + fileName, sw.ToString());

You can't write files to the client from the server, would you want a website writing files to your machine?
 
Share this answer
 
Its resolved, by modifying some code and some new line of code


//style to format numbers to string
       string style = @"<style> .textmode { mso-number-format:\@; } </style>";
       Response.Write(style);
       string strPath = HostingEnvironment.ApplicationPhysicalPath + @"TempFiles/";
       string fileName = Uid + "ContactList.xls";
       File.WriteAllText(strPath + fileName, sw.ToString());
       Response.Clear();
       Response.Write("{\"success\":true,\"isSuccess\":true,\"fileName\":\"" + fileName + "\"}");

       //Response.Output.Write(sw.ToString());
       //Response.Flush();
       //Response.End();
 
Share this answer
 

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