网页怎么转成Word文档,大家讨论一下
请教高手,普通的html页面怎么转成word格式啊,就相当于把网页复制粘贴到word中,用程序如何实现?
------解决方案--------------------
//过程
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=result.xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition", "attachment;filename=Resume.doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
//关闭控件的视图状态
source.Page.EnableViewState = false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
Response.Write(writer.ToString());
Response.End();
}
public enum DocumentType
{
Word,
Excel
}
//调用
protected void Button1_Click(object sender, EventArgs e)
{
ExpertControl(this, DocumentType.Word);
}
------解决方案--------------------
C# code
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+fname+".doc");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-word";
Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
this.Page.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();