我嘗試在彈出窗口中輸出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();
}
定義'Popup'並告訴我們您輸出CSV的方式。 –
顯示一些代碼。如何顯示csv文件? –
嘗試使用「text/csv」作爲MIME類型(Response.ContentType)。我無法找到「text /逗號分隔值」的任何RFC(RFC 4180是text/csv:http://tools.ietf.org/html/rfc4180) –