(1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf
(1) a.首先添加引用:
using Microsoft.Office.Core; using Word = Microsoft.Office.Interop.Word;
b.其次编写文档转换的方法:
1 /// <summary> 2 /// word转成html 3 /// </summary> 4 /// <param name="path">要转换的文档的路径</param> 5 /// <param name="savePath">转换成的html的保存路径</param> 6 /// <param name="wordFileName">转换后html文件的名字</param> 7 private static void WordToHtml(string path, string savePath, string wordFileName) 8 { 9 Word.ApplicationClass word = new Word.ApplicationClass(); 10 Type wordType = word.GetType(); 11 Word.Documents docs = word.Documents; 12 Type docsType = docs.GetType(); 13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {(object)path, true, true }); 14 Type docType = doc.GetType(); 15 string strSaveFileName = savePath + wordFileName + ".html"; 16 object saveFileName = (object)strSaveFileName; 17 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML }); 18 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null); 19 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 20 } 21 /// <summary> 22 /// excel 转换为html 23 /// </summary> 24 /// <param name="path">要转换的文档的路径</param> 25 /// <param name="savePath">转换成的html的保存路径</param> 26 /// <param name="wordFileName">转换后html文件的名字</param> 27 public static void ExcelToHtml(string path, string savePath, string wordFileName) 28 { 29 string str = string.Empty; 30 Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application(); 31 Microsoft.Office.Interop.Excel.Workbook workbook = null; 32 Microsoft.Office.Interop.Excel.Worksheet worksheet = null; 33 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); 34 worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; 35 object htmlFile = savePath + wordFileName + ".html"; 36 object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml; 37 workbook.SaveAs(htmlFile, 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); 38 object osave = false; 39 workbook.Close(osave, Type.Missing, Type.Missing); 40 repExcel.Quit(); 41 } 42 /// <summary> 43 /// ppt转换为html 44 /// </summary> 45 /// <param name="path">要转换的文档的路径</param> 46 /// <param name="savePath">转换成的html的保存路径</param> 47 /// <param name="wordFileName">转换后html文件的名字</param> 48 public static void PPTToHtml(string path, string savePath, string wordFileName) 49 { 50 Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application(); 51 string strSourceFile = path; 52 string strDestinationFile = savePath + wordFileName + ".html"; 53 Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(strSourceFile, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); 54 prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue); 55 prsPres.Close(); 56 ppApp.Quit(); 57 }
c.接下来就是调用方法:WordToHtml(string path, string savePath, string wordFileName);ExcelToHtml(string path, string savePath, string wordFileName);PPTToHtml(string path, string savePath, string wordFileName)实现相应文档的转换.