public static void ConvertToPDFWithPrinter(String filePath, String destpath)
        {
            // file exist & check type
            if (!filePath.IsNormalized() || !File.Exists(filePath))
            {
                throw new Exception("未指定文件");
            }
            if (!filePath.EndsWith("doc") && !filePath.EndsWith("docx"))
            {
                throw new Exception("文件非 word 类型");
            }

            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true });
            doc.Application.ActivePrinter = "Microsoft Print to PDF";
            Type docType = doc.GetType();

            docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument, destpath });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
        /// <summary>
        /// Word转成Html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成html的保存路径</param>
        /// <param name="wordFileName">转换成html的文件名字</param>
        public static void Word2Html(string path, string savePath, string wordFileName)
        {
            MyWord.Application word = new MyWord.Application();
            Type wordType           = word.GetType();

            MyWord.Documents docs     = word.Documents;
            Type             docsType = docs.GetType();

            MyWord.Document doc = (MyWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            try
            {
                Type   docType         = doc.GetType();
                string strSaveFileName = System.IO.Path.Combine(savePath, $"{wordFileName}.html");
                object saveFileName    = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, MyWord.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch
            { }
            finally
            {
                doc  = null;
                docs = null;
                word = null;
            }
        }
Beispiel #3
0
        ///<summary>
        /// 将Word文件转换为HTML文件
        ///</summary>
        ///<param name="source_path">Word文件路径(绝对)</param>
        ///<param name="target_path">生成的Html文件路径(绝对),路径的目录结构必须存在,否则保存失败</param>
        ///<returns>是否执行成功</returns>
        public static bool toHtml(string source_path, string target_path)
        {
            Word.Application wordApp  = new Word.Application();
            Type             wordType = wordApp.GetType();

            Word.Documents docs     = wordApp.Documents;
            Type           docsType = docs.GetType();

            try
            {
                if (File.Exists(target_path))
                {
                    File.Delete(target_path);
                }
                Word.Document doc     = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new Object[] { source_path, true, true });
                Type          docType = doc.GetType();
                docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { target_path, Word.WdSaveFormat.wdFormatFilteredHTML });
                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordApp, null);
                try
                {
                    wordApp.Quit();
                }
                catch { }
                Thread.Sleep(3000);
            }
        }
Beispiel #4
0
        /// <summary>
        /// 需要转换的Word文档路径
        /// </summary>
        /// <param name="fileName">完整路径</param>
        /// <returns>HTML页面路径</returns>
        private static string WordToHtml(object fileName)
        {
            Word.Application word     = new Word.Application();
            Type             wordType = word.GetType();

            Word.Documents docs = word.Documents;

            //打开文件
            Type docsType = docs.GetType();

            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs, new object[] { fileName, true, true });

            //转换格式,另存
            Type   docType          = doc.GetType();
            string wordSaveFileName = fileName.ToString();
            string htmlSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf(".") + 1) + "html";
            object saveFileName     = (object)htmlSaveFileName;

            docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", BindingFlags.InvokeMethod, null, doc, null);

            //退出Word
            wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);

            return(saveFileName.ToString());
        }
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName, string htmlFileName)
        {
            object saveFileName = "";

            try
            {
                //在此处放置用户代码以初始化页面
                Word.ApplicationClass word = new Word.ApplicationClass();
                Type           wordType    = word.GetType();
                Word.Documents docs        = word.Documents;
                //打开文件
                Type          docsType = docs.GetType();
                Word.Document doc      = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
                //转换格式,另存为
                Type   docType          = doc.GetType();
                string wordSaveFileName = wordFileName.ToString();


                string strSaveFileName = htmlFileName;
                saveFileName = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch (Exception ex)
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), null, string.Format("<script>alert('{0}');</script>", ex.Message.ToString()));
            }
            return(saveFileName.ToString());
        }
Beispiel #6
0
 /// <summary>
 /// word转成html
 /// </summary>
 /// <param name="wordFileName">绝对地址</param>
 public static string wordToHtml(object wordFileName)
 {
     try
     {
         //判断文件夹是否存在(不存在创建一个)
         if (!Directory.Exists(HttpContext.Current.Server.MapPath("/WordToHtml")))
         {
             new DirectoryInfo(HttpContext.Current.Server.MapPath("/WordToHtml")).Create();
         }
         //在此处放置用户代码以初始化页面
         Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
         Type wordType = word.GetType();
         Microsoft.Office.Interop.Word.Documents docs = word.Documents;
         //打开文件
         Type docsType = docs.GetType();
         Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
         //转换格式,另存为
         Type   docType          = doc.GetType();
         string wordSaveFileName = wordFileName.ToString();
         string strSaveFileName  = "";
         strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
         object saveFileName = (object)strSaveFileName;
         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
         //退出 Word
         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
         return(saveFileName.ToString());
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog(Convert.ToString("Word错误:" + ex.Message + "------------------------------------" + ex.StackTrace));
         throw new Exception(ex.Message, ex);
     }
 }
Beispiel #7
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName, string htmlFileName)
        {
            object saveFileName = "";

            try
            {
                //在此处放置用户代码以初始化页面
                Word.ApplicationClass word = new Word.ApplicationClass();
                if (word == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("word is null", "Error");

                    return("");
                }
                Type           wordType = word.GetType();
                Word.Documents docs     = word.Documents;

                if (docs == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("docs is null", "Error");
                    return("");
                }
                //打开文件
                Type          docsType = docs.GetType();
                Word.Document doc      = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });

                if (doc == null)
                {
                    ServiceAppSetting.LoggerHander.Invoke("doc is null", "Error");
                    return("");
                }
                //转换格式,另存为
                Type   docType          = doc.GetType();
                string wordSaveFileName = wordFileName.ToString();


                string strSaveFileName = htmlFileName;
                saveFileName = (object)strSaveFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                //退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            }
            catch (Exception ex)
            {
                ServiceAppSetting.LoggerHander.Invoke(string.Format("<script>alert('{0}!');</script>", ex.Message), "Error");
            }

            return(saveFileName.ToString());
        }
Beispiel #8
0
        /// <summary>生成PDF文件</summary>
        public override bool Execute(string sourceFile, string destFile)
        {
            bool success = true;

            Microsoft.Office.Interop.Word.ApplicationClass application = null;
            Microsoft.Office.Interop.Word.Documents        documents   = null;
            Microsoft.Office.Interop.Word.Document         document    = null;
            try
            {
                application = new Microsoft.Office.Interop.Word.ApplicationClass();
                //Type wordType = application.GetType();
                documents = application.Documents;

                // 打开文件
                Type docsType = documents.GetType();
                document = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                         System.Reflection.BindingFlags.InvokeMethod, null, documents, new Object[] { sourceFile, true, true });

                Type docType = document.GetType();
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                     null, document, new object[] { destFile, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            }
            catch (Exception)
            {
                success = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                    Marshal.ReleaseComObject(document);
                }
                if (application != null)
                {
                    application.Quit();
                    Marshal.ReleaseComObject(application);
                }
                //关闭文档
                //docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, document, new object[] { null, null, null });

                //退出 Word
                //wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, application, null);
            }

            return(success);
        }
    /// <summary>
    /// Word转成Html
    /// </summary>
    /// <param name="path">要转换的文档的路径</param>
    /// <param name="savePath">转换成html的保存路径</param>
    /// <param name="wordFileName">转换成html的文件名字</param>
    public static void Word2Html(string path, string savePath, string wordFileName)
    {
        Word.ApplicationClass word = new Word.ApplicationClass();
        Type wordType = word.GetType();

        Word.Documents docs     = word.Documents;
        Type           docsType = docs.GetType();

        Word.Document doc             = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
        Type          docType         = doc.GetType();
        string        strSaveFileName = savePath + wordFileName + ".html";
        object        saveFileName    = (object)strSaveFileName;

        docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
        docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
    }
Beispiel #10
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="path">要转换的文档的路径</param>
        /// <param name="savePath">转换成的html的保存路径</param>
        /// <param name="wordFileName">转换后html文件的名字</param>
        //private static void WordToHtml(string path, string savePath, string wordFileName)
        //{
        //    Word.ApplicationClass word = new Word.ApplicationClass();
        //    Type wordwordType = word.GetType();
        //    Word.Documents docs = word.Documents;
        //    Type docsdocsType = docs.GetType();
        //    Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
        //    Type docdocType = doc.GetType();
        //    string strSaveFileName = savePath + wordFileName + ".html";
        //    object saveFileName = (object)strSaveFileName;
        //    docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
        //    docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
        //    wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        //}
        private static void WordToHtml(string path)
        {
            Word.Application word     = new Word.Application();
            Type             wordType = word.GetType();

            Word.Documents docs     = word.Documents;
            Type           docsType = docs.GetType();

            Word.Document doc             = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
            Type          docdocType      = doc.GetType();
            string        strSaveFileName = path.Substring(0, path.Length - 3) + ".html";
            object        saveFileName    = (object)strSaveFileName;

            docsType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docsType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
Beispiel #11
0
        public static string WordToHtml(object wordfilename)
        {
            //在此处放置用户代码以初始化页面
            word.Application word     = new word.Application();
            Type             wordtype = word.GetType();

            word.Documents docs = word.Documents;
            //打开文件
            Type docstype = docs.GetType();

            word.Document doc = (word.Document)docstype.InvokeMember("open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[] { wordfilename, true, true });
            //转换格式,另存为
            Type   doctype          = doc.GetType();
            string wordsavefilename = wordfilename.ToString();
            string strsavefilename  = wordsavefilename.Substring(0, wordsavefilename.LastIndexOf('.')) + ".html";
            object savefilename     = (object)strsavefilename;

            doctype.InvokeMember("saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            doctype.InvokeMember("close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            // 退出 word
            wordtype.InvokeMember("quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return(savefilename.ToString());
        }
Beispiel #12
0
        public void PrintWord(string sourcePath, string targetPath)

        {
            Word.ApplicationClass word = new Word.ApplicationClass();

            Type wordType = word.GetType();



            //打开WORD文档

            Word.Documents docs = word.Documents;

            Type docsType = docs.GetType();

            object objDocName = sourcePath;

            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });



            object printFileName = targetPath;


            //打印输出到指定文件

            //可以使用 doc.PrintOut();方法,次方法调用中的参数设置较繁琐,建议使用 Type.InvokeMember 来调用时可以不用将PrintOut的参数设置全,只设置4个主要参数

            Type docType = doc.GetType();

            docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, Word.WdPrintOutRange.wdPrintAllDocument, printFileName });


            //退出WORD

            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
    /// <summary>
    /// print Word
    /// </summary>
    /// <param name="wordfile">Fullpath and file name</param>
    /// <param name="printer">Printer name</param>
    public static void Print(string wordfile, string printer)
    {
        oWord.ApplicationClass word = new oWord.ApplicationClass();
        Type wordType = word.GetType();

        //Open WORD
        oWord.Documents docs       = word.Documents;
        Type            docsType   = docs.GetType();
        object          objDocName = wordfile;

        oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { objDocName, true, true });

        doc.Application.ActivePrinter = printer;

        //Print to File
        //used doc.PrintOut() and Type.InvokeMember
        Type   docType       = doc.GetType();
        object printFileName = wordfile + ".xps";

        docType.InvokeMember("PrintOut", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { false, false, oWord.WdPrintOutRange.wdPrintAllDocument, printFileName });

        //Exit WORD
        wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
    }
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName"></param>
        private string WordToHtml(object wordFileName)
        {
            //在此处放置用户代码以初始化页面
            Word.Application word     = new Word.Application();
            Type             wordType = word.GetType();

            Word.Documents docs = word.Documents;
            //打开文件
            Type docsType = docs.GetType();

            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
            //转换格式,另存为
            Type   docType          = doc.GetType();
            string wordSaveFileName = wordFileName.ToString();
            string sExt             = wordSaveFileName.Substring(wordSaveFileName.LastIndexOf('.'));
            string strSaveFileName  = wordSaveFileName.Substring(0, wordSaveFileName.Length - sExt.Length) + ".html";
            object saveFileName     = (object)strSaveFileName;

            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
            //退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            return(saveFileName.ToString());
        }
Beispiel #15
0
        //上传文件并转换为html wordToHtml(wordFilePath)
        ///<summary>
        ///上传文件并转存为html
        ///</summary>
        ///<param name="wordFilePath">word文档在客户机的位置</param>
        ///<returns>上传的html文件的地址</returns>
        public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)
        {
            MSWord.ApplicationClass word = new MSWord.ApplicationClass();
            Type wordType = word.GetType();

            MSWord.Documents docs = word.Documents;

            // 打开文件
            Type docsType = docs.GetType();

            //应当先把文件上传至服务器然后再解析文件为html
            string filePath = uploadWord(wordFilePath);

            //判断是否上传文件成功
            if (filePath == "0")
            {
                return("0");
            }
            //判断是否为word文件
            if (filePath == "1")
            {
                return("1");
            }

            object fileName = filePath;

            MSWord.Document doc = (MSWord.Document)docsType.InvokeMember("Open",
                                                                         System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

            // 转换格式,另存为html
            Type docType = doc.GetType();

            string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
                              System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();

            // 判断指定目录下是否存在文件夹,如果不存在,则创建
            if (!Directory.Exists(Server.MapPath("~\\html")))
            {
                // 创建up文件夹
                Directory.CreateDirectory(Server.MapPath("~\\html"));
            }

            //被转换的html文档保存的位置
            string ConfigPath   = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
            object saveFileName = ConfigPath;

            /*下面是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, MSWord.WdSaveFormat.wdFormatFilteredHTML });

            //关闭文档
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, new object[] { null, null, null });

            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
            //转到新生成的页面
            return("/" + filename + ".html");
        }
        public string GetPathByDocToHTML(string strFile)
        {
            if (!File.Exists(strFile))
            {
                return("文件不存在");
            }

            ApplicationClass word     = new ApplicationClass();
            Type             wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;

            // 打开文件
            Type docsType = docs.GetType();

            object fileName = strFile;

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                                                       System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

            // 转换格式,另存为html
            Type docType = doc.GetType();
            //给文件重新起名
            string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
                              System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();

            string   strFileFolder = "/html/";
            DateTime dt            = DateTime.Now;
            //以yyyymmdd形式生成子文件夹名
            string strFileSubFolder = dt.Year.ToString();

            strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
            strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
            string strFilePath = strFileFolder + strFileSubFolder + "/";

            // 判断指定目录下是否存在文件夹,如果不存在,则创建
            if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(strFilePath)))
            {
                // 创建up文件夹
                Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(strFilePath));
            }

            //被转换的html文档保存的位置
            // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
            string ConfigPath   = System.Web.HttpContext.Current.Server.MapPath(strFilePath + filename + ".html");
            object saveFileName = ConfigPath;

            /*下面是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, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });


            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

            //关闭文档
            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, new object[] { null, null, null });

            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

            //转化HTML页面统一编码格式
            TransHTMLEncoding(ConfigPath);

            //拷贝文件到研究院官网目录下
            CopyDir(System.Web.HttpContext.Current.Server.MapPath("/html/"), YjyWebPath + "html");
            return(strFilePath + filename + ".html");
        }
Beispiel #17
0
        ///<summary>
        /// 把Word文件转换成为PDF格式文件
        ///</summary>
        private void DOCConvertToPDF()
        {
            Word.ApplicationClass application = new Word.ApplicationClass();
            Word.Documents        docs        = application.Documents;
            Type wordtype = application.GetType();
            Type docstype = docs.GetType();

            try
            {
                Word.Document document = (Word.Document)docstype.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[] { sourcePath, true, true });
                Type          doctype  = document.GetType();
                doctype.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, document, new object[] { targetPath, Word.WdSaveFormat.wdFormatPDF });
            }
            catch (Exception e)
            {
                throw e;
            }
            finally {
                wordtype.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, application, null);
            }


            /*
             * bool result = false;
             * Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
             * object paramMissing = Type.Missing;
             * Word.ApplicationClass wordApplication = new Word.ApplicationClass();
             * Word.Document wordDocument = null;
             * try
             * {
             *  object paramSourceDocPath = sourcePath;
             *  string paramExportFilePath = targetPath;
             *
             *  Word.WdExportFormat paramExportFormat = exportFormat;
             *  bool paramOpenAfterExport = false;
             *  Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
             *  Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
             *  int paramStartPage = 0;
             *  int paramEndPage = 0;
             *  Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
             *  bool paramIncludeDocProps = true;
             *  bool paramKeepIRM = true;
             *  Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
             *  bool paramDocStructureTags = true;
             *  bool paramBitmapMissingFonts = true;
             *  bool paramUseISO19005_1 = false;
             *
             *  wordDocument = wordApplication.Documents.Open(
             *  ref paramSourceDocPath, ref paramMissing, ref paramMissing,
             *  ref paramMissing, ref paramMissing, ref paramMissing,
             *  ref paramMissing, ref paramMissing, ref paramMissing,
             *  ref paramMissing, ref paramMissing, ref paramMissing,
             *  ref paramMissing, ref paramMissing, ref paramMissing,
             *  ref paramMissing);
             *
             *  if (wordDocument != null)
             *      wordDocument.ExportAsFixedFormat(paramExportFilePath,
             *      paramExportFormat, paramOpenAfterExport,
             *      paramExportOptimizeFor, paramExportRange, paramStartPage,
             *      paramEndPage, paramExportItem, paramIncludeDocProps,
             *      paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
             *      paramBitmapMissingFonts, paramUseISO19005_1,
             *      ref paramMissing);
             *  result = true;
             * }
             * catch
             * {
             *  result = false;
             * }
             * finally
             * {
             *  if (wordDocument != null)
             *  {
             *      wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
             *      wordDocument = null;
             *  }
             *  if (wordApplication != null)
             *  {
             *      wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
             *      wordApplication = null;
             *  }
             *  GC.Collect();
             *  GC.WaitForPendingFinalizers();
             *  GC.Collect();
             *  GC.WaitForPendingFinalizers();
             * }
             * return result;*/
        }
Beispiel #18
0
        /// <summary>
        /// Word转HTMl
        /// </summary>
        /// <param name="path">word文件路径 带盘符</param>
        /// <returns></returns>
        public static string WordToHtml(string wordPath)
        {
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Object oMissing = System.Reflection.Missing.Value;
            // 打开文件
            Type docsType = docs.GetType();

            //应当先把文件上传至服务器然后再解析文件为html
            string filePath = wordPath;

            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                                                       System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true });

            // 转换格式,另存为html
            Type docType = doc.GetType();

            //被转换的html文档保存的位置
            string saveFileName = "";
            string ext          = EKFileUpload.getExtend(wordPath).ToLower();

            if (ext == "doc")
            {
                saveFileName = wordPath.Replace(".doc", "");
            }
            else if (ext == "docx")
            {
                saveFileName = wordPath.Replace(".docx", "");
            }
            else
            {
            }

            //另存为html页面
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

            //doc.Close(ref oMissing, ref oMissing, ref oMissing);
            try
            {
                docs.Close(ref oMissing, ref oMissing, ref oMissing);
                word.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
            catch (Exception ex)
            {
                new MS_LogBLL().AddLogAdmin(MS_LogBLL.LogLevel.ERROR, "word转换失败" + ex.Message);
            }
            GC.Collect();

            //读取html 页面
            string html = "";

            //特别处理.有"."号的的文件名.直接读.  不用加.htm
            if (saveFileName.Contains("."))
            {
                html = EKFile.ReadFile(saveFileName);
            }
            else
            {
                html = EKFile.ReadFile(saveFileName + ".htm");
            }

            //补全图片地址
            html = EKHtml.ImageWebRoot(html, "http://" + System.Web.HttpContext.Current.Request.Url.Authority + "/files/temp/", "<img[\\s\\S]+?src=\"(?<url>.+?)\">");

            //保存页面图片
            html = EKHtml.SaveImage(html, DateTime.Now, "<img[\\s\\S]+?src=\"(?<url>.+?)\">");

            //删除临时文件.也可保留
            if (saveFileName.Contains("."))
            {
                EKFile.DeleteFile(saveFileName);
            }
            else
            {
                EKFile.DeleteFile(saveFileName + ".htm");
            }
            //删除目录
            if (EKFile.ExistsDirectory(saveFileName + ".files"))
            {
                EKFile.DeleteDirectoryAnd(saveFileName + ".files");
                EKFile.DeleteDirectory(saveFileName + ".files");
            }
            EKFile.DeleteFile(wordPath);

            return(html);
        }
Beispiel #19
0
        /// <summary>
        /// word转换html
        /// </summary>
        /// <param name="strFile">全路径</param>
        /// <returns></returns>
        private string GetPathByDocToHTML(string strFile)
        {
            try
            {
                if (string.IsNullOrEmpty(strFile))
                {
                    return("0"); //没有文件
                }

                Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
                Type wordType = word.GetType();
                Microsoft.Office.Interop.Word.Documents docs = word.Documents;

                // 打开文件
                Type docsType = docs.GetType();

                object fileName = strFile;

                Microsoft.Office.Interop.Word.Document doc =
                    (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                  System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

                // 转换格式,另存为html
                Type docType = doc.GetType();
                //给文件重新起名
                string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() +
                                  System.DateTime.Now.Day.ToString() +
                                  System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() +
                                  System.DateTime.Now.Second.ToString();

                string   strFileFolder = "~/html/";
                DateTime dt            = DateTime.Now;
                //以yyyymmdd形式生成子文件夹名
                string strFileSubFolder = dt.Year.ToString();
                strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
                strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
                string strFilePath = strFileFolder + strFileSubFolder + "/";
                // 判断指定目录下是否存在文件夹,如果不存在,则创建
                if (!Directory.Exists(Server.MapPath(strFilePath)))
                {
                    // 创建up文件夹
                    Directory.CreateDirectory(Server.MapPath(strFilePath));
                }

                //被转换的html文档保存的位置
                // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
                string ConfigPath   = Server.MapPath(strFilePath + filename + ".html");
                object saveFileName = ConfigPath;

                /*下面是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, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

                //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

                //关闭文档
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                     null, doc, new object[] { null, null, null });

                // 退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                //转到新生成的页面
                //return ("/" + filename + ".html");

                //转化HTML页面统一编码格式
                TransHTMLEncoding(ConfigPath);

                File.Delete(strFile); //删除临时保存的文件

                return(strFilePath + filename + ".html");
            }
            catch (Exception ex)
            {
                using (FileStream fs = new FileStream(Server.MapPath("~/log/") + DateTime.Now.ToString("yyyyMMdd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(ex.ToString() + "\n" + ex.Message.ToString() + "\n" + ex.StackTrace + "\n" + ex.InnerException + "\n");
                    };
                };

                File.Delete(strFile); //删除临时保存的文件
                Page.RegisterStartupScript("alt", "<script>alert('转Html出错了。')</script>");
                return("0");          //没有文件
            }
        }