Asp.net 导出 .html,.txt 等格式的文件

      Asp.net 导出 excel 只要设置 Response 的 ContentType 为 excel 所要求的 ContentType 就行了,导出来的 excel 可以给用户下载保存。但是怎样导出 .html 等格式的文件呢?我将 Response 的 ContentType 设置成了 html 的 ContentType,但是点导出之后,生成的文件会被浏览器直接打开。难道 web 程序不能导出 .html 文件让用户下载?答案是否定的。我在用 Gmail 的时候就上传过 html 的附件,并且可以把附件的 html 文件下载下来。那就可以肯定 web 程序导出 html 文件是可以实现的!
       实现的方法其实很简单,不过需要使用一点小技巧。

StringWriter sw = new StringWriter(); 
sw.WriteLine(
"html export test");
sw.Close(); 
Response.Clear();
Response.AddHeader(
"Content-Disposition""attachment; filename=test.html"); 
Response.ContentType 
= "application/ms-excel";//定义成 excel 需要让用户自己下载
Response.ContentEncoding = Encoding.GetEncoding("gb2312");
Response.Write(sw); 
Response.End(); 

       以上实现方式参考了博客园 LoveCherry 的文章。具体地址链接一下找不到了。
原文地址:https://www.cnblogs.com/focus/p/1053346.html