2013-07-16 79 views
0

我需要將Excel中下載到特定的文件夾如何下載excel文件到指定文件夾在asp.net

例如:d:\電子郵件

現在我能下載Excel文件下載....但我需要在d下載:\電子郵件

,這是我的代碼來創建Excel文件:

   protected void UploadDataTableToExcel(DataTable dtRecords) 
     { 
     string XlsPath = Server.MapPath(@"~/Add_data/test.xls"); 
     string attachment = string.Empty; 
     if (XlsPath.IndexOf("\\") != -1) 
     { 
      string[] strFileName = XlsPath.Split(new char[] { '\\' }); 
      attachment = "attachment; filename=" + strFileName[strFileName.Length - 1]; 
     } 
     else 
      attachment = "attachment; filename=" + XlsPath; 
     try 
     { 
      Response.ClearContent(); 
      Response.AddHeader("content-disposition", attachment); 
      Response.ContentType = "application/vnd.ms-excel"; 
      string tab = string.Empty; 

      foreach (DataColumn datacol in dtRecords.Columns) 
      { 
       Response.Write(tab + datacol.ColumnName); 
       tab = "\t"; 
      } 
      Response.Write("\n"); 

      foreach (DataRow dr in dtRecords.Rows) 
      { 
       tab = ""; 
       for (int j = 0; j < dtRecords.Columns.Count; j++) 
       { 
        Response.Write(tab + Convert.ToString(dr[j])); 
        tab = "\t"; 
       } 

       Response.Write("\n"); 
      } 
      Response.End(); 
     } 
     catch (Exception ex) 
     { 
      //Response.Write(ex.Message); 
     } 
      } 
+0

您可以在哪裏下載它? – Aisha

+0

我可以在「下載」中下載 – user2536023

+0

如果它適合您,請發送您的解決方案! – Aisha

回答

0

您可以使用手動

下載後的文件移動
string sourceFile = @"C:\Users\test\Documents\Downloads\test.xls"; 
string destinationFile = @"D:\emails\test.xls"; 
// To move a file or folder to a new location: 
System.IO.File.Move(sourceFile, destinationFile); 
相關問題