Office系列在线预览

最近客户有个需求,需要在线预览PPT、Excel、Word,开始打算用第三方组建去读取 office系列,然后生成html,这样的话样式相当于丢了,只剩下数据,而且第三方组件对office版本支持不够完善,最关键的是还是样式丢了!

最后决定,用户在上传的过程中调用office API里面的saveAs,自动生成了静态html,预览的时候就直接访问的该html页面。

[a].WordToHtml

复制代码
复制代码
 1  public static string WordToHtml(string path, string savePath, string wordFileName)
 2         {
 3 
 4             //在此处放置用户代码以初始化页面
 5             Microsoft.Office.Interop.Word.Application word = new Word.Application();
 6 
 7             Type wordType = word.GetType();
 8 
 9             Word.Documents docs = word.Documents;
10 
11             //打开文件
12             Type docsType = docs.GetType();
13             Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { path, truetrue });
14 
15             //转换格式,另存为
16             Type docType = doc.GetType();
17 
18             string wordSaveFileName = savePath;
19 
20             string strSaveFileName = savePath+wordFileName + ".html";
21 
22             object saveFileName = (object)strSaveFileName;
23 
24             docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
25 
26             docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
27 
28             //退出 Word
29             wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
30 
31             return saveFileName.ToString();
32         }
复制代码
复制代码

 

[b].ExcelToHtml

复制代码
复制代码
 1  public static void ExcelToHtml(string path,string savePath, string wordFileName)
 2         {
 3             string str = string.Empty;
 4             Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
 5             Microsoft.Office.Interop.Excel.Workbook workbook = null;
 6             Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
 7             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);
 8             worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
 9             object htmlFile =savePath+wordFileName+ ".html";
10             object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
11             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);
12             object osave = false;
13             workbook.Close(osave, Type.Missing, Type.Missing);
14             repExcel.Quit();
15            
16         }
复制代码
复制代码

 

[c].PPTToHtml

复制代码
复制代码
 public static  void PPTToHtml(string path, string savePath, string wordFileName)
        {
            Microsoft.Office.Interop.PowerPoint.Application ppApp 
= new Microsoft.Office.Interop.PowerPoint.Application();
            
string strSourceFile = path;
            
string strDestinationFile = savePath+wordFileName+".html";
            Microsoft.Office.Interop.PowerPoint.Presentation prsPres 
= ppApp.Presentations.Open(strSourceFile, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
            prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
            prsPres.Close();
            ppApp.Quit();

        }
复制代码
复制代码

 

当完成该功能的时候,上帝那边传来噩耗,他们表示不愿意在服务器上安装office,所以我们继续寻找解决方案······

出处:http://www.cnblogs.com/iamzhanglei/archive/2011/08/10/2133840.html

posted on   jack_Meng  阅读(523)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2013年5月 >
28 29 30 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩