一个简单的文件输出
一:
1、定义文档类型、字符编码
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-word";
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档(默认是application/ms-html )
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fileName.ToString(),System.Text.Encoding.UTF8));
// attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=aa.doc 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
this.EnableViewState = false;
2、定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
3、将目标数据绑定到输入流输出
this.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
二:或用Response.WriteFile()
1、同上。
2、省略。
3、修改为:
Response.WriteFile(filePath + fileName,true);
Response.Flush();
Response.Close();
Response.End();
1、定义文档类型、字符编码
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-word";
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档(默认是application/ms-html )
Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(fileName.ToString(),System.Text.Encoding.UTF8));
// attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=aa.doc 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
this.EnableViewState = false;
2、定义一个输入流
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
3、将目标数据绑定到输入流输出
this.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
二:或用Response.WriteFile()
1、同上。
2、省略。
3、修改为:
Response.WriteFile(filePath + fileName,true);
Response.Flush();
Response.Close();
Response.End();
浙公网安备 33010602011771号