2011-06-22 45 views
2

我嘗試在彈出窗口中輸出CSV文件。我可以在主窗口中輸出CSV,但不能彈出。 我使用相同的代碼。例如;我使用表單來輸出CSV文件。當這個表格是主窗口時,可以輸出CSV文件。當我使用這個表單作爲彈出窗口時,它不能輸出。我能怎麼做?使用C#在ASP.Net中輸出彈出窗口的CSV文件

這是我的CSV輸出功能:

public void OutputCSV(DataTable dtValue, string strFilename) 
{ 

     if (TextUtility.IsNullOrEmpty(strFilename)) 
     { 
      throw new I01Exception(Enums.ExType.System, "9909_35"); 
     } 

     DataTable dt = dtValue; 
     StringBuilder sb = new System.Text.StringBuilder(); 
     string strCh = ","; 

     //項目名 
     for (int k = 0; k < dt.Columns.Count; k++) 
     { 
      sb.Append("\"" + dt.Columns[k].Caption + "\"" + strCh); 
     } 
     sb.Remove(sb.Length - 1, 1); 

     //データ 
     foreach (DataRow row in dt.Rows) 
     { 
      sb.AppendLine(string.Empty); 
      for (int i = 0; i < dt.Columns.Count; i++) 
      { 
       sb.Append("\"" + row[i].ToString() + "\"" + strCh); 
      } 
     } 

     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.Buffer = true; 
     HttpContext.Current.Response.AppendHeader("content-disposition", string.Format("attachment; filename={0}", HttpUtility.UrlEncode(strFilename + ".csv"))); 
     HttpContext.Current.Response.ContentType = "text/comma-separated-values"; 
     HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("shift-jis"); 
     HttpContext.Current.Response.Write(sb.ToString()); 
     HttpContext.Current.Response.Flush(); 
     HttpContext.Current.ApplicationInstance.CompleteRequest(); 
     HttpContext.Current.Response.End(); 

    } 
+0

定義'Popup'並告訴我們您輸出CSV的方式。 –

+0

顯示一些代碼。如何顯示csv文件? –

+1

嘗試使用「text/csv」作爲MIME類型(Response.ContentType)。我無法找到「text /逗號分隔值」的任何RFC(RFC 4180是text/csv:http://tools.ietf.org/html/rfc4180) –

回答

0

只需添加一個HREF與tartget =「_新的」指向您的網址從網頁。如果你需要一個彈出窗口,他最好的選擇是使用一個jQuery的流行庫,他們採取externla網址。如果你正在嘗試使用本地JavaScript彈出窗口,這是一個僅用於文本的對話框?

0

繼續從西蒙湯普森的答案(好回答西蒙)。

我覺得你從錯誤的一面接近這一點。彈出窗口不是在服務器端創建的,而是在客戶端以純html或javascript創建的。

因此,例如:

<a href="http://google.com" target="_blank">open google in a popup</a> 

以下是有關彈出的一些詳細信息:http://www.quirksmode.org/js/popup.html

的怪異模式教程是一個有用的介紹,但是實現我不會使用這種方法,而使用更「不顯眼」的方法(但是,一旦你掌握了彈出基礎知識,我會在稍後看一下)。