2013-08-22 190 views
1

我想將網格視圖轉換爲excel並將excel表格下載到我的計算機在我的c#應用程序中的特定文件夾。如何避免將文件下載到下載文件夾,並只下載到其他特定文件夾#

問題是:該文件正在下載兩個地方。目標文件夾和下載文件夾。

這個代碼是:

private void ExportToExcel(GridView GrdView, string fname) 
{ 
    Response.Clear(); 
    Response.AddHeader("content-disposition", "inline;filename=" + fname + ".xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/OCTET-STREAM"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = 
    new HtmlTextWriter(stringWrite); 
    GridView1.RenderControl(htmlWrite); 
    string renderedGridView = stringWrite.ToString(); 
    File.WriteAllText(@"C:\\Users\abc\\Desktop\" + fname + ".xls", renderedGridView); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
} 

如何避免文件得到下載到下載文件夾? 請幫忙! 謝謝

+0

參見:http://stackoverflow.com/questions/6773866/download-file-and-automatically-save-it-to-folder –

回答

2

答案是選擇一個:將控件渲染到一個字符串並輸出到Response或WriteAllText。如果您想使用WriteAllText將文件保存到確定的位置,則執行該操作並彈出一條通知給用戶該文件已保存。

+0

謝謝你,它的工作精美!我刪除了所有的迴應聲明和使用的WriteAllText 。 – LearningToCode

相關問題