将word、excel、ppt转换成html,调用office的API;转换后的html样式会丢失

  1 using System;
  2 using System.Collections.Generic;
  3 
  4 using System.IO;
  5 using System.Threading;
  6 using System.Collections;
  7 using Excel = Microsoft.Office.Interop.Excel;
  8 using Word = Microsoft.Office.Interop.Word;
  9 
 10 /// <summary>
 11 /// Summary description for Class1
 12 /// </summary>
 13 public class Office2Html
 14 {
 15     public Office2Html()
 16     {
 17     }
 18 
 19     public static void WordToHtml(string filePath)
 20     {
 21         Word.Application word = new Word.Application();
 22         Type wordType = word.GetType();
 23         Word.Documents docs = word.Documents;
 24         Type docsType = docs.GetType();
 25         Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)filePath, true, true });
 26         Type docType = doc.GetType();
 27         string strSaveFileName = filePath.ToLower().Replace(Path.GetExtension(filePath).ToLower(), ".html");
 28         object saveFileName = (object)strSaveFileName;
 29         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
 30         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
 31         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
 32         Thread.Sleep(3000);//为了使退出完全,这里阻塞3秒
 33     }
 34 
 35 
 36     public static void WordToHtml(string filePath, string htmlFilePaht)
 37     {
 38         Word.Application word = new Word.Application();
 39         Type wordType = word.GetType();
 40         Word.Documents docs = word.Documents;
 41         Type docsType = docs.GetType();
 42         Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)filePath, true, true });
 43         Type docType = doc.GetType();
 44         string strSaveFileName = htmlFilePaht;
 45         object saveFileName = (object)strSaveFileName;
 46         docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
 47         docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
 48         wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
 49         Thread.Sleep(3000);//为了使退出完全,这里阻塞3秒
 50     }
 51 
 52 
 53 
 54     public static void ExcelToHtml(string filePath)
 55     {
 56         string str = string.Empty;
 57         Excel.Application oApp = new Excel.Application();
 58         Excel.Workbook oBook = null;
 59         Excel.Worksheet oSheet = null;
 60         Excel.Workbooks oBooks = null;
 61 
 62         oBooks = oApp.Application.Workbooks;
 63         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);
 64         oSheet = (Excel.Worksheet)oBook.Worksheets[1];
 65         object htmlFile = filePath.ToLower().Replace(Path.GetExtension(filePath).ToLower(), ".html");
 66         object ofmt = Excel.XlFileFormat.xlHtml;
 67         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);
 68 
 69         NAR(oSheet);
 70         GC.Collect();
 71         object osave = false;
 72         oBook.Close(osave, Type.Missing, Type.Missing);
 73         GC.Collect();
 74         NAR(oBook);
 75         GC.Collect();
 76         NAR(oBooks);
 77         GC.Collect();
 78         oApp.Quit();
 79         NAR(oApp);
 80         GC.Collect();
 81         KillProcess("EXCEL");
 82         Thread.Sleep(3000);//保证完全关闭
 83     }
 84 
 85 
 86 
 87     public static void ExcelToHtml(string filePath, string htmlFilePaht)
 88     {
 89         string str = string.Empty;
 90         Excel.Application oApp = new Excel.Application();
 91         Excel.Workbook oBook = null;
 92         Excel.Worksheet oSheet = null;
 93         Excel.Workbooks oBooks = null;
 94 
 95         oBooks = oApp.Application.Workbooks;
 96         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);
 97         oSheet = (Excel.Worksheet)oBook.Worksheets[1];
 98         object htmlFile = htmlFilePaht;
 99         object ofmt = Excel.XlFileFormat.xlHtml;
100         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);
101 
102         NAR(oSheet);
103         GC.Collect();
104         object osave = false;
105         oBook.Close(osave, Type.Missing, Type.Missing);
106         NAR(oBook);
107         GC.Collect();
108         NAR(oBooks);
109         GC.Collect();
110         oApp.Quit();
111         NAR(oApp);
112         GC.Collect();
113         KillProcess("EXCEL");
114         Thread.Sleep(3000);//保证完全关闭
115 
116     }
117 
118     //依据时间杀灭进程
119     private static void KillProcess(string processName)
120     {
121         System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(processName);
122         foreach (System.Diagnostics.Process p in process)
123         {
124             if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
125             {
126                 p.Kill();
127             }
128         }
129     }
130 
131     //关闭对象
132     private static void NAR(object o)
133     {
134         try
135         {
136             while (System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0) ;
137         }
138         catch { }
139         finally
140         {
141             o = null;
142         }
143     }
144 }

 

     注意:请设置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 

 

程序员的基础教程:菜鸟程序员

posted on 2013-03-15 10:01  itprobie-菜鸟程序员  阅读(2691)  评论(0编辑  收藏  举报