1. 程式人生 > >線上預覽文件html版

線上預覽文件html版

複製程式碼
 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 }
複製程式碼