如何在应用程序文件夹中保存excel文件? [英] How to save excel file in application folder?

查看:136
本文介绍了如何在应用程序文件夹中保存excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我花了很多时间在这个问题上,你能解决吗?



问题是我无法将文件保存在应用程序文件夹中。

ok。

让我知道我在这里做什么,我从数据库中获取数据表并将其转换为excel文件。这个excel文件我要保存在应用程序文件夹中。

下面是我的代码...

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





我的尝试:



我正在尝试将excel文件保存在应用程序文件夹中。



What I have tried:

I am trying to save excel file in application folder.

推荐答案

你不能从服务器向客户端写文件,你想要吗?一个网站将文件写入你的机器?
You can't write files to the client from the server, would you want a website writing files to your machine?


通过修改一些代码和一些新的代码来解决它的问题





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


这篇关于如何在应用程序文件夹中保存excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆