当前位置:编程学习 > C#/ASP.NET >>

如何将多个HTML页面内容转换为一个WORD文档中去

如何将多个HTML页面内容转换为一个WORD文档中去?
文档原文是WORD(4页),我们先把4页内容分别做了4个HTML模板,然后又要把这4个HTML页面里的内容输出到一个WORD里去。网上都是WORD转换HTML,那位有做过HTML转WORD的?请指教,谢谢
--------------------编程问答--------------------
private void Page_Load(object sender, System.EventArgs e)
 {
  // 在此处放置用户代码以初始化页面
    Word.ApplicationClass word = new Word.ApplicationClass();
    Type wordType = word.GetType();
    Word.Documents docs = word.Documents;
    // 打开文件
    Type docsType = docs.GetType();
    object fileName = "d:\\tmp\\aaa.doc";
    Word.Document doc = (Word.Document)docsType.InvokeMember("Open", 
    System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {fileName, true, true});
          
    // 转换格式,另存为
    Type docType = doc.GetType();
    object saveFileName = "d:\\tmp\\aaa.html";
    //下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
    //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
     null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
    ///其它格式:
    ///wdFormatHTML
    ///wdFormatDocument
    ///wdFormatDOSText
    ///wdFormatDOSTextLineBreaks
    ///wdFormatEncodedText
    ///wdFormatRTF
    ///wdFormatTemplate
    ///wdFormatText
    ///wdFormatTextLineBreaks
    ///wdFormatUnicodeText
    docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
     null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML});
    // 退出 Word
    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
     null, word, null);
}
 
--------------------编程问答--------------------
 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=result.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
        }
        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
           //在web窗体中添加一个按钮使用该方法
        private void Button1_Click(object sender, System.EventArgs e)
        {
                ExpertControl(this, DocumentType.Word);
        }
    }
--------------------编程问答-------------------- http://www.cnblogs.com/mc-dragon/archive/2007/01/16/622080.html
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,