2015-10-15 84 views
0
string attachment = "attachment; filename=Myfile.csv"; 
HttpContext.Current.Response.Clear(); 
HttpContext.Current.Response.ClearHeaders(); 
HttpContext.Current.Response.ClearContent(); 
HttpContext.Current.Response.AddHeader("content-disposition", attachment); 
HttpContext.Current.Response.ContentType = "text/csv"; 
HttpContext.Current.Response.AddHeader("Pragma", "public"); 

// My logic goes here to write file. 

// Now I want to copy above file to another file. 

string filepath = Server.MapPath("~/ExportedFiles/") +"Newfile.csv"; 
var ExcelFile = File.Create(filepath); 
ExcelFile.Close(); 
HttpContext.Current.Response.TransmitFile(filepath); 
HttpContext.Current.Response.Flush(); 

上述代碼無法正常工作,請給我建議。謝謝。如何在C#中編寫文件?

+4

的可能的複製[最簡單的方法讀取和寫入文件(http://stackoverflow.com/questions/7569904/easiest-way-讀取和寫入文件) – Keale

+2

請定義「不工作」 - 編譯錯誤,運行時錯誤,意外輸出?加上關於那個錯誤的任何細節。 –

+0

沒有得到任何錯誤。但它不寫入Newfile.csv – Rao

回答

0

使用代碼波紋管而不是:var ExcelFile = File.Create(filepath);在C#中使用 WriteAllLines指令來創建文件或更改內容文件

File.WriteAllLines(filepath, data); 
+0

請添加一些說明,而不是僅僅發佈答案,這將有助於他人避免將來出現的問題。 – VPK