工作中用到的一个把Word Excel PPT转换成HTML操作类,记录一下。可用作在线阅览。

 

    public class OfficeDocument
    {
        /// <summary>
        /// Word生成HTML文件
        /// </summary>
        /// <param name="sourcefilePath">源文档路径</param>
        /// <param name="targetfilePath">目标文档路径</param>
        /// <returns>返回:true - 成功;false - 失败;</returns>
        public static bool WordToHtml(string sourcefilePath, string targetfilePath)
        {
            SqlRunTimeNote note = new SqlRunTimeNote("Word生成HTML文件开始......");
            note.WirteToLog();

            bool operationResult = false;

            try
            {
                if (!File.Exists(targetfilePath))
                {
                    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                    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[] { sourcefilePath, true, true });
                    Type doctype = doc.GetType();
                    doctype.InvokeMember("Saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { targetfilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
                    doctype.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
                    wordtype.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

                    operationResult = true;

                    note = new SqlRunTimeNote("Word生成HTML文件成功......");
                    note.WirteToLog();
                }
                else
                {
                    //已经转成Html了,操作结果也为true
                    operationResult = true;
                }
            }
            catch (Exception ex)
            {
                operationResult = false;

                note = new SqlRunTimeNote("Word生成HTML文件:" + ex.Message);
                note.WirteToLog();
            }


            return operationResult;
        }

        /// <summary>
        /// PPT生成HTML文件
        /// </summary>
        /// <param name="sourcefilePath">源文档路径</param>
        /// <param name="targetfilePath">目标文档路径</param>
        /// <returns>返回:true - 成功;false - 失败;</returns>
        public static bool PPTtoHtml(string sourcefilePath, string targetfilePath)
        {
            SqlRunTimeNote note = new SqlRunTimeNote("PPT生成HTML文件开始......");
            note.WirteToLog();

            bool operationResult = false;

            try
            {
                if (!File.Exists(targetfilePath))
                {
                    Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
                    Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(sourcefilePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                    prsPres.SaveAs(targetfilePath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
                    prsPres.Close();
                    ppApp.Quit();

                    operationResult = true;

                    note = new SqlRunTimeNote("PPT生成HTML文件成功......");
                    note.WirteToLog();
                }
                else
                {
                    //已经转成Html了,操作结果也为true
                    operationResult = true;
                }
            }
            catch (Exception ex)
            {
                operationResult = false;

                note = new SqlRunTimeNote("PPT生成HTML文件:" + ex.Message);
                note.WirteToLog();
            }

            return operationResult;
        }

        /// <summary>
        /// Excel生成HTML文件
        /// </summary>
        /// <param name="sourcefilePath">源文档路径</param>
        /// <param name="targetfilePath">目标文档路径</param>
        /// <returns>返回:true - 成功;false - 失败;</returns>
        public static bool ExcelToHtml(string sourcefilePath, string targetfilePath)
        {
            SqlRunTimeNote note = new SqlRunTimeNote("Excel生成HTML文件开始......");
            note.WirteToLog();

            bool operationResult = false;

            try
            {
                if (!File.Exists(targetfilePath))
                {
                    Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel.Workbook workbook = null;
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = null;

                    //打开文件,sourcefilePath是文件路径  
                    workbook = repExcel.Application.Workbooks.Open(sourcefilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
                    object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;

                    //进行另存为操作
                    workbook.SaveAs(targetfilePath, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    object osave = false;

                    //逐步关闭所有使用的对象  
                    workbook.Close(osave, Type.Missing, Type.Missing);
                    repExcel.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
                    worksheet = null;

                    //垃圾回收  
                    GC.Collect();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
                    workbook = null;
                    GC.Collect();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
                    GC.Collect();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
                    repExcel = null;
                    GC.Collect();

                    ////依据时间杀灭进程  
                    //System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("Excel");
                    //foreach (System.Diagnostics.Process p in process)
                    //{
                    //    if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
                    //    {
                    //        p.Kill();
                    //    }
                    //}

                    operationResult = true;

                    note = new SqlRunTimeNote("Excel生成HTML文件成功......");
                    note.WirteToLog();
                }
                else
                {
                    //已经转成Html了,操作结果也为true
                    operationResult = true;
                }
            }
            catch (Exception ex)
            {
                operationResult = false;

                note = new SqlRunTimeNote("Excel生成HTML文件:" + ex.Message);
                note.WirteToLog();
            }

            return operationResult;
        }
    }

 

posted on 2015-04-29 16:28  Mack.Meng  阅读(744)  评论(2)    收藏  举报