如何在物理路径中下载空Excel文件

时间:2015-07-20 07:16:16

标签: c# excel

在这里,我想在链接按钮单击“物理路径中的事件”中下载空Excel文件。  我的代码是

  HttpResponse httpResponse = HttpContext.Current.Response;
        httpResponse.Clear();
        httpResponse.ClearContent();
        httpResponse.ClearHeaders();         

        httpResponse.AddHeader("content-disposition", "attachment;filename=" + txt1.Text + ".xls");            
        string strpath = ConfigurationManager.AppSettings["Downloads"].ToString() + txt1.Text;           
        httpResponse.ContentType = "application/vnd.ms-excel;";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        httpResponse.Write(style);
        httpResponse.Output.Write(sw.ToString());

        httpResponse.Flush();
        httpResponse.Close();
        httpResponse.End();

Web.Config中的我的AddKey是

<add key="Downloads" value="C:\Users\MyName\Desktop\Downloads"/>

这里我如何给出这个添加键值

1 个答案:

答案 0 :(得分:0)

也许这可以帮助你WebClient.DownloadFile

using (var client = new WebClient())
{
    client.DownloadFile(theUrl,
        Path.Combine(ConfigurationManager.AppSettings["Downloads"].ToString(), "myXls.xls"));
}