C#下载

public void Download() {
Response.Clear();
Response.ClearHeaders();

//读取数据内容或者从文件读取或者数据库
String strD = "这是测试的内容";

//文件
StreamReader sr = new StreamReader("C:/object.txt",Encoding.Default);
string o = sr.ReadToEnd();

//生成个CSV(和excel基本没有区别,但是操作起来非常的方便)
/*
*-----------------------------------------------------------
*CSV中的换行是\n,下个单元格是,简单吧.
*-----------------------------------------------------------
*/
string e = "";
e += "标题1,标题2,标题3\n";
e += "内容1,内容2,内容3\n";

Response.ContentType = "application/vnd.ms-excel";// "text/plain";可以查询我MIME文章中的头信息
//Response.ContentEncoding = Encoding.GetEncoding("GB2312");// 如果需要发送的话
String httpHeader = "attachment;filename=download.csv";
Response.AddHeader("Content-Disposition", httpHeader);
Response.Write(e);
Response.Flush();
Response.End();
}

C#下载_第1张图片

你可能感兴趣的:(C#)