2011/9/19 11:17:03

真正的HTML转WORD文档代码

真正的HTML转WORD文档代码

HTML转word,网络上很多代码,但是都是以直接改后缀名或者改html表头实现的,这种转换后的doc其实是假的word格式,这种格式如果是直接用office打开,问题不大,最多选择一下打开方式,其它的word自己会处理。如果你还想要对这个doc做处理,例如用程序读取或者转换格式的话,那这样就糟糕了,完全读取不了的,因此这种方式其实很误导人,后来我尝试了下,用office里面的方法去转换,就比较完美。代码贴出来。
 
using Microsoft.Office.Interop.Word;

/// <summary>
        /// 另存为
        /// </summary>
        /// <param name="fileName">输入文件</param>
        /// <param name="pFileName">输出文件</param>
        public static void SaveAsWord(string fileName, string pFileName)
        {

            object missing = System.Reflection.Missing.Value;
            object readOnly = false;
            object isVisible = true;
            object file1 = fileName;
            object html1 = pFileName;


            object format = WdSaveFormat.wdFormatDocument;


            ApplicationClass oWordApp = new ApplicationClass();
            oWordApp.Visible = true;


            Document oWordDoc = oWordApp.Documents.Open(ref   file1, ref   format, ref   readOnly, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref missing);
            oWordDoc.SaveAs(ref   html1, ref   format, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing, ref   missing);


            oWordDoc.Close(ref     missing, ref     missing, ref     missing);
            oWordDoc = null;
            oWordApp.Application.Quit(ref   missing, ref   missing, ref   missing);
            oWordApp = null;
           

        }
 
 
而对于很多人说的图片路径问题,如果你为了保证完全可行,你可以把图片全部放本地,例如index.html,index.files文件夹是图片,然后html里面的图片路径全部指向这里,转换时候,word会自动帮你打包进去。