我需要在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.
就像这样使用
Response.Clear();
Response.ContentType = "application/CSV";
Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + ".csv\"");
Response.Write(t.ToString());
Response.End();
多年来,我一直在为此打磨一套完美的头文件,在我所知道的所有浏览器中都能出色地工作
// 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");
在ASP.net MVC中,你可以使用FileContentResult和File方法:
public FileContentResult DownloadManifest() {
byte[] csvData = getCsvData();
return File(csvData, "text/csv", "filename.csv");
}
使用text/csv作为内容类型。