将word、excel、ppt转换成html,调用office的API;转换后的html样式会丢失
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
using System; using System.Collections.Generic; using System.IO; using System.Threading; using System.Collections; using Excel = Microsoft.Office.Interop.Excel; using Word = Microsoft.Office.Interop.Word; /// <summary> /// Summary description for Class1 /// </summary> public class Office2Html { public Office2Html() { } public static void WordToHtml(string filePath) { Word.Application word = new Word.Application(); 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)filePath, true, true }); Type docType = doc.GetType(); string strSaveFileName = filePath.ToLower().Replace(Path.GetExtension(filePath).ToLower(), ".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); Thread.Sleep(3000);//为了使退出完全,这里阻塞3秒 } public static void WordToHtml(string filePath, string htmlFilePaht) { Word.Application word = new Word.Application(); 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)filePath, true, true }); Type docType = doc.GetType(); string strSaveFileName = htmlFilePaht; 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); Thread.Sleep(3000);//为了使退出完全,这里阻塞3秒 } public static void ExcelToHtml(string filePath) { string str = string.Empty; Excel.Application oApp = new Excel.Application(); Excel.Workbook oBook = null; Excel.Worksheet oSheet = null; Excel.Workbooks oBooks = null; oBooks = oApp.Application.Workbooks; oBook = oBooks.Open(filePath, 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); oSheet = (Excel.Worksheet)oBook.Worksheets[1]; object htmlFile = filePath.ToLower().Replace(Path.GetExtension(filePath).ToLower(), ".html"); object ofmt = Excel.XlFileFormat.xlHtml; oBook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); NAR(oSheet); GC.Collect(); object osave = false; oBook.Close(osave, Type.Missing, Type.Missing); GC.Collect(); NAR(oBook); GC.Collect(); NAR(oBooks); GC.Collect(); oApp.Quit(); NAR(oApp); GC.Collect(); KillProcess("EXCEL"); Thread.Sleep(3000);//保证完全关闭 } public static void ExcelToHtml(string filePath, string htmlFilePaht) { string str = string.Empty; Excel.Application oApp = new Excel.Application(); Excel.Workbook oBook = null; Excel.Worksheet oSheet = null; Excel.Workbooks oBooks = null; oBooks = oApp.Application.Workbooks; oBook = oBooks.Open(filePath, 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); oSheet = (Excel.Worksheet)oBook.Worksheets[1]; object htmlFile = htmlFilePaht; object ofmt = Excel.XlFileFormat.xlHtml; oBook.SaveAs(htmlFile, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); NAR(oSheet); GC.Collect(); object osave = false; oBook.Close(osave, Type.Missing, Type.Missing); NAR(oBook); GC.Collect(); NAR(oBooks); GC.Collect(); oApp.Quit(); NAR(oApp); GC.Collect(); KillProcess("EXCEL"); Thread.Sleep(3000);//保证完全关闭 } //依据时间杀灭进程 private static void KillProcess(string processName) { System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(processName); foreach (System.Diagnostics.Process p in process) { if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5) { p.Kill(); } } } //关闭对象 private static void NAR(object o) { try { while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) ; } catch { } finally { o = null; } } } |
注意:请设置web.config的system.web加点中添加一下配置,操作excel或word需要管理员的权限,否则报错“否则会提示检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。”
方案一:
<identity impersonate=”true” userName=”服务器管理员用户名” password=”服务器管理员”/>
方案二:
1:在服务器上安装office的Excel软件.
2:在”开始”->”运行”中输入dcomcnfg.exe启动”组件服务”
3:依次双击”组件服务”->”计算机”->”我的电脑”->”DCOM配置”
4:在”DCOM配置”中找到”Microsoft Excel 应用程序”,在它上面点击右键,然后点击”属性”,弹出”Microsoft Excel 应
用程序属性”对话框
5:点击”标识”标签,选择”交互式用户”
6:点击”安全”标签,在”启动和激活权限”上点击”自定义”,然后点击对应的”编辑”按钮,在弹出的”安全性”对话框中填加
一个”NETWORK SERVICE”用户(注意要选择本计算机名),并给它赋予”本地启动”和”本地激活”权限.
7:依然是”安全”标签,在”访问权限”上点击”自定义”,然后点击”编辑”,在弹出的”安全性”对话框中也填加一个”NETWORK
SERVICE”用户,然后赋予”本地访问”权限.
这样,我们便配置好了相应的Excel的DCOM权限.
在项目引用中右击选择添加引用,选择COM里面选择Microft Office 12.0 object Library和Microft Excel 12.0 object Library分别点确定即可!同样如果要引用World选Microft World 12.0 object Library! 2003/2007共通处理方式 分别为11或12版本 添加.net中Microsoft.Office.Interop.excel; 添加.net中Office