将页面输出到文件用法

 
查看文章
 
将页面输出到文件用法
2009-09-17 12:55
将页面输出到文件用法

一、定义文档类型、字符编码  

Response.Clear();

Response.Buffer= true;

Response.Charset="utf-8";  

//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开

//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm  

Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");

Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");  

//Response.ContentType指定文件类型 可以为application/ms-excel    application/ms-word    application/ms-txt    application/ms-html    或其他浏览器可直接支持文档 

Response.ContentType = "application/ms-excel";

this.EnableViewState = false;  

二、定义一个输入流  

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

三、将目标数据绑定到输入流输出  

this.RenderControl(oHtmlTextWriter);   

//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件  

Response.Write(oStringWriter.ToString());

Response.End();  

总结:本例程在Microsoft Visual Studio .NET 2005平台下测试通过,适用于C#和VB,根据需要存放的内容可吧this改为服务器控件。

解决中文乱码方案:
protected void Button4_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode("中文", System.Text.Encoding.UTF8) + ".xls");//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8      

// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Panel1.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream.
//this.GridView1.RenderControl(oHtmlTextWriter);//将gridview输出,你也可以类似的写TextBox1.RenderControl...
Response.Output.Write(oStringWriter.ToString());//Output the stream.
Response.Flush();
Response.End();
}
//End of the Print data code.

//重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}

-------------------------------------------------------------------------------------------

Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode("中文", System.Text.Encoding.UTF8) + ".xls");//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8      

// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Panel1.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream.
//this.GridView1.RenderControl(oHtmlTextWriter);//将gridview输出,你也可以类似的写TextBox1.RenderControl...
Response.Output.Write(oStringWriter.ToString());//Output the stream.
Response.Flush();
Response.End();
}
//End of the Print data code.

//重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}
posted @ 2009-09-24 09:01  张宏宇  阅读(308)  评论(0编辑  收藏  举报