您的位置:首页 > 编程语言 > ASP

ASP.NET实现将网页内容输出到WORD并下载到本地

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

关键函数:

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”/>不然会执行出错。

在导出Execl或Word的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示。

下面的2中方法是在网上找到的:

修改web.config(不推荐)

<pages enableEventValidation ="false" ></pages>


直接在导出Execl的页面修改

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="ExportWordByIO.aspx.cs" Inherits="_Default" EnableEventValidation = "false"  %>


 实现思想:

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

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

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

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

  

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm7.aspx.cs" Inherits="TH.WebForm7"  EnableEventValidation = "false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>导出word</title>
</head>
<body>

<form id="form1" runat="server">
<div>
秋天来临了天空像一块覆盖大地的蓝宝石。村外那个小池塘睁着碧澄澄的眼睛,凝望着这美好的天色。一对小白鹅侧着脑袋欣赏自己映在水里的影子。山谷里枫树的叶子,不知是否喝了过量的酒,红的像一团火似的。村前村后的稻子,低着头弯着腰,在秋风中默默地等待着人们去收割,半空中,排着“人”字形的雁群,高兴的唱着歌,告别人们,向天边慢慢飞去……
<%--<img src="Image/sy_71272488121.jpg" />--%>
</div>
<asp:Button ID="Button1" runat="server" Text="导出" onclick="Button1_Click" />
</form>

</body>
</html>


后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TH
{
public partial class WebForm7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
/// <summary>
/// 实现导出word
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//设置http的头信息,编码格式
//缓冲输出
HttpContext.Current.Response.Buffer = true;
//清空页面输出流
HttpContext.Current.Response.Clear();
//设置输出流的HTTP字符集
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.ContentType = "applic
ad35
ation/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();
}
}
}


这种方法,无法导出带有图片的文档,因技术有限故无法实现。有人若知,望留言告知。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net