asp.net实现将网页内容输出到word并下载到本地

时间:2023-03-09 07:48:19
asp.net实现将网页内容输出到word并下载到本地

  个人觉得要实现这个功能如果没有类库提供的几个关键函数,还是比较繁琐的。所以首先介绍几个将要在代码中使用的关键函数和参数,然后再说函数实现、注意问题等。

  关键函数:

  1.函数原型:Response.AppendHeader(name,value);

    本例中使用: Response.AppendHeader("Content-Disposition", "attachment;filename=fileDown.doc");

  说明:将http头添加到输出流,name 为Http头,value为Http头的值,可以实现刷新页面,页面跳转,文件下载等,就看你name的值是什么。例如在本例中使用name为Content-Disposition:

    Content-Disposition:是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活浏览器文 件下载对话框,它的文件名框自动填充了头中指定的文件名,来确保浏览器弹出下载对话框。

在本例中value的值为attachment;filename=fileDown.doc:

    attachment: attachment 参数表示作为附件下载,您可以改成 online在线打开 ,filename自定义下载的文件名称,文件后缀为想要下载的文件类型,后面有说明。

  2.Response.ContentType

    本例中设置:Response.ContentType = "application/ms-word";

  说明:指定文件类型 可以为application/ms-excel , application/ms-word,application/ms-txt,application/ms-html或其他浏览器可直接支持文档。

  3.System.Web.UI.HtmlTextWriter类

  说明:将标记字符和文本写入到 ASP.NET 服务器控件输出流,也就是用于把HTML内容输出到服务器控件输出流的一个类。在本例中是将要下载的页面内容输出到一个StringWriter对象中。

  4.RenderControl(HtmlWriter);

  说明:将服务器控件的内容输出到所提供的HtmlWriter对象中,在本例中是将要下载的页面内容输出到HtmlWriter中。

  注意:在本例中需要将页面的EnableEventValidation="false",<pages enableEventValidation="false"/>不然会执行出错。

  实现思想:

  第一步:设置Response的格式,缓冲,编码等,调用AppendHeader函数用于弹出浏览器保存文件对话框,并设置文件名字、类型以及保存方式(在线浏览还是作为附件保存)。

  第二步:初始化HtmlWriter,将下载页面内容输出给HtmlWriter,并将内容输出到一个StringWriter对象中。

  第三步:将StringWriter对象的值赋值给一个string对象,然后操作字符串对象,截取想要下载的内容。

  第四步:调用Response.Write将string对象输出到第一步指定的文件中。

  代码:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebPageContentDownload
{
public partial class DownloadWebPageContent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void download_Click(object sender, EventArgs e)
{
//设置Http的头信息,编码格式
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=fileDown.doc");
//关闭控件的视图状态 ,如果仍然为true,RenderControl将启用页的跟踪功能,存储与控件有关的跟踪信息
this.EnableViewState = false;
//将要下载的页面输出到HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
this.RenderControl(htmlWriter);
//提取要输出的内容
string pageHtml = writer.ToString();
int startIndex = pageHtml.IndexOf("<div style=\"margin: 0 auto;\" id=\"mainContent\">");
int endIndex = pageHtml.LastIndexOf("</div>");
int lenth = endIndex - startIndex;
pageHtml = pageHtml.Substring(startIndex, lenth);
//输出
HttpContext.Current.Response.Write(pageHtml.ToString());
HttpContext.Current.Response.End();
}
}
}