zoukankan      html  css  js  c++  java
  • Word、Exce、PPT转html文档

            /// <summary>
            ///  word 转换为html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成的html的保存路径</param>
            /// <param name="wordFileName">转换后html文件的名字</param>
            public static void WordToHtml(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);
            }
    
            /// <summary>
            /// excel 转换为html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成的html的保存路径</param>
            /// <param name="wordFileName">转换后html文件的名字</param>
            public static void ExcelToHtml(string path, string savePath, string wordFileName)
            {
                string str = string.Empty;
                Excel.Application repExcel = new Excel.Application();
                Excel.Workbook workbook = null;
                Excel.Worksheet worksheet = null;
                workbook = repExcel.Application.Workbooks.Open(path, 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 = (Excel.Worksheet)workbook.Worksheets[1];
                object htmlFile = savePath + wordFileName + ".html";
                object ofmt = Excel.XlFileFormat.xlHtml;
                workbook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 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();
            }
    
            /// <summary>
            /// ppt转换为html
            /// </summary>
            /// <param name="path">要转换的文档的路径</param>
            /// <param name="savePath">转换成的html的保存路径</param>
            /// <param name="wordFileName">转换后html文件的名字</param>
            public static void PPTToHtml(string path, string savePath, string wordFileName)
            {
                PowerPoint.Application ppApp = new PowerPoint.Application();
                string strSourceFile = path;
                string strDestinationFile = savePath + wordFileName + ".html";
                PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                prsPres.SaveAs(strDestinationFile, PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
                prsPres.Close();
                ppApp.Quit();
            }

    需要添加下列com引用:




      

    进行using简化:

    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;
  • 相关阅读:
    面试题 16.07. 最大数值
    461. 汉明距离
    1290. 二进制链表转整数
    1486. 数组异或操作
    1480. 一维数组的动态和
    面试题 17.04. 消失的数字
    626. 换座位
    125. 验证回文串
    530. 二叉搜索树的最小绝对差
    ASP.NET页面之间传递值的几种方式
  • 原文地址:https://www.cnblogs.com/bo10296/p/4303836.html
Copyright © 2011-2022 走看看