我需要在HTTP响应中发送一个CSV文件。如何将输出响应设置为CSV格式?
这是行不通的:
Response.ContentType = "application/CSV";
我需要在HTTP响应中发送一个CSV文件。如何将输出响应设置为CSV格式?
这是行不通的:
Response.ContentType = "application/CSV";
当前回答
就像这样使用
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(t.ToString());
Response.End();
其他回答
对于c# MVC 4.5,你需要这样做:
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + fileName + ".csv\"");
Response.Write(dataNeedToPrint);
Response.End();
return new EmptyResult(); //this line is important else it will not work.
使用text/csv作为内容类型。
多年来,我一直在为此打磨一套完美的头文件,在我所知道的所有浏览器中都能出色地工作
// these headers avoid IE problems when using https:
// see http://support.microsoft.com/kb/812935
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=$filename.csv");
根据RFC 4180, CSV的MIME类型为text/ CSV。
我建议在'myfilename.cvs'前面插入一个'/'字符
Response.AddHeader("Content-Disposition", "attachment;filename=/myfilename.csv");
我希望你能取得更好的成绩。