/// <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;