在线预览文档html版

(1).将文档转换为html,只支持支持office文档 (2).将文档转换为flash,实现类似百度文库的效果,除支持office文档外还支持pdf

  (1) a.首先添加引用:

using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

  b.其次编写文档转换的方法:

复制代码
 1     /// 
 2     /// word转成html
 3     /// 
 4     /// 要转换的文档的路径
 5     /// 转换成的html的保存路径
 6     /// 转换后html文件的名字
 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     /// 
22     /// excel 转换为html
23   /// 
24   /// 要转换的文档的路径
25   /// 转换成的html的保存路径
26   /// 转换后html文件的名字
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     /// 
43   /// ppt转换为html
44   /// 
45   /// 要转换的文档的路径
46   /// 转换成的html的保存路径
47   /// 转换后html文件的名字
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)实现相应文档的转换.

你可能感兴趣的:(NET)