Office文档WEB端在线浏览(转换成Html)
最近在做了一个项目,要求是对Office文档在线预览。下面给大家分享一下我的方法。
1.第一种方法(不建议使用)
我是在网上搜了一个利用COM组件对office文档进行转换,但是此方法必须要装Office办公软件,而且容易与电脑上的WPS冲突,还有一系列的版本问题。我电脑上装的是Office2010,没有装WPS,所以直接可以使用。具体方法如下:
Microsoft Office 14.0 Object Library
Microsoft Word 14.0 Object Library
Microsoft Excel 14.0 Object Library
Microsoft PowerPoint 14.0 Object Library

然后添加一个Office2HtmlHelper类,用于转换文件操作
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word; namespace OfficeToHtml.Utils
{
public class Office2HtmlHelper
{
/// <summary>
/// Word转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void Word2Html(string path, string savePath, string wordFileName)
{ Word.ApplicationClass word = new Word.ApplicationClass();
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)path, true, true });
Type docType = doc.GetType();
string strSaveFileName = savePath + wordFileName + ".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);
}
/// <summary>
/// Excel转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void Excel2Html(string path, string savePath, string wordFileName)
{
string str = string.Empty;
Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
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);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[];
object htmlFile = savePath + wordFileName + ".html";
object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
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);
object osave = false;
workbook.Close(osave, Type.Missing, Type.Missing);
repExcel.Quit();
}
/// <summary>
/// ppt转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void PPT2Html(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, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
}
}
}
这样在上传文件的时候就可以使用Office2HtmlHelper类进行转换操作了,(控制器代码)
//创建文件夹
string rootFolder = System.Configuration.ConfigurationManager.AppSettings["UploadFileRootPath"];
string fileType = file.FileName.Split('.').Last();
string folderPath = rootFolder + "\\\\" + UPLOADS + "\\\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\\\";
//文件查看目录
string viewFolderPath = rootFolder + "\\\\" + HTMLUPLOADS + "\\\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\\\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (!Directory.Exists(viewFolderPath))
{
Directory.CreateDirectory(viewFolderPath);
}
string fileName = Guid.NewGuid().ToString();
filePath = folderPath + fileName + "." + fileType;
file.SaveAs(filePath);
switch (fileType)
{
case "docx":
case "doc":
Office2HtmlHelper.Word2Html(filePath, viewFolderPath, fileName);
viewPath = viewFolderPath + fileName + "." + "html";
break;
case "xlsx":
case "xls":
Office2HtmlHelper.Excel2Html(filePath, viewFolderPath, fileName);
viewPath = viewFolderPath + fileName + "." + "html";
break;
case "ppt":
case "pptx":
Office2HtmlHelper.PPT2Html(filePath, viewFolderPath, fileName);
viewPath = viewFolderPath + fileName + "." + "html";
break;
default:
break;
}

但是这样的话就必须要装Office软件才可以调用COM组件了。所以为了避免这种不友好的事情发生,最终选择了使用Aspose动态链接库
使用方法不变,只不过是转换方式变了,相比使用COM组件来说更加简便了一些:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
//using Microsoft.Office.Core;
//using Word = Microsoft.Office.Interop.Word;
using Aspose.Cells;
using Aspose.Slides.Pptx; namespace CqscSecurityApplication.Utils
{
public class Office2HtmlHelper
{
/// <summary>
/// Word转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void Word2Html(string path, string savePath, string wordFileName)
{ //Word.ApplicationClass word = new Word.ApplicationClass();
//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)path, true, true });
//Type docType = doc.GetType();
//string strSaveFileName = savePath + wordFileName + ".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);
Aspose.Words.Document doc = new Aspose.Words.Document(path);//通过path(文件原始源路径)获取文档内容
wordFileName = wordFileName + ".html";
string savePathss = Path.Combine(savePath, wordFileName);//合并转换后html文件路径
doc.Save(savePathss,Aspose.Words.SaveFormat.Html);//转换为html格式
}
/// <summary>
/// Excel转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void Excel2Html(string path, string savePath, string wordFileName)
{
//string str = string.Empty;
//Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
//Microsoft.Office.Interop.Excel.Workbook workbook = null;
//Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
//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);
//worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
//object htmlFile = savePath + wordFileName + ".html";
//object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//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);
//object osave = false;
//workbook.Close(osave, Type.Missing, Type.Missing);
//repExcel.Quit();
Workbook workbook=new Workbook(path);
wordFileName = wordFileName + ".html";
string savePathss = Path.Combine(savePath, wordFileName);
workbook.Save(savePathss, SaveFormat.Html);
}
/// <summary>
/// ppt转成Html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savePath">转换成html的保存路径</param>
/// <param name="wordFileName">转换成html的文件名字</param>
public static void PPT2Html(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, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse); //prsPres.SaveAs(strDestinationFile, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoTrue);
//prsPres.Close();
//ppApp.Quit();
PresentationEx pres=new PresentationEx(path);
wordFileName = wordFileName + ".html";
string savePathss = Path.Combine(savePath, wordFileName);
pres.Save(savePathss, Aspose.Slides.Export.SaveFormat.Html);
}
}
}
总之使用很简单,有遇到同样问题的同学,欢迎交流;
Office文档WEB端在线浏览(转换成Html)的更多相关文章
- OFFICE 文档转换为html在线预览
OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的 ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
- Java实现web在线预览office文档与pdf文档实例
https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...
- 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览
条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上) 实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...
- [Office Web Apps]实现在线office文档预览
摘要 在使用office web apps实现office文档在线预览的时候,需要注意的地方. web api web api作为owa在线预览服务回调的接口,这里面核心代码片段如下: using H ...
- 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
- [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
- Java版office文档在线预览
java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
http://www.cnblogs.com/wuhuacong/p/3871991.html 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览 在博客园很多文章 ...
随机推荐
- spring学习第8天(advisor)
1.关于之前的一个错误:aop的输出顺序,实际上官方文档上并没有说是否按照书写顺序输出的,有可能你1000次里面800次按顺序输出,200是随机输出的.<aop:aspect>有一个ord ...
- C语言-浮点类型
C语言-浮点类型 浮点类型 在0的两侧有一小块区域,这个区域非常接近0,但是不等于0,是float(表达范围数量级10^-38^)或者double(达范围数量级10^-308^)无法表达的,而0是可以 ...
- 五、CI框架之通过带路径的view视图路径访问
一.如果需要现在的某个目录的View界面,需要在controller中写入文件路径 二.访问http://127.0.0.1/CodeIgniter-3.1.10/index.php/显示如下: 不忘 ...
- MVC三层架构在各框架中的特征
转一篇写得很棒的文章:https://my.oschina.net/win199176/blog/208171?p=7&temp=1495894148424 1.基于web开发中最原始的jsp ...
- Essay写作常见问题解析
Essay是西方大学的主要考核形式之一.其理念是考核学生对资料信息的吸取和观点的输出能力.可是对于刚踏入美国大学的国际留学生来说,写Essay就像是一种水土不服.各种不适和挣扎是不可避免的!今天小编来 ...
- maven的理解和使用
一.maven是什么? maven是项目管理工具 二.maven为什么要用? 在做开发的时候常常会用到外部的工具包(jar包),这就需要你一个一个的去他们的官网下工具包,然后在项目里依赖他们,比较的麻 ...
- tf.summary可视化参数
1.tf.summary.scalar('accuracy', accuracy) 损失值.准确率随着迭代次数的进行,其指标变化情况:一般在画loss,accuary时会用到这个函数. 2.tenso ...
- (简单模拟)P1540 机器翻译
题解: #include<iostream>#include<cmath>using namespace std; int main(){ int m,n; cin>&g ...
- 浅谈无参数RCE
0x00 前言 这几天做了几道无参数RCE的题目,这里来总结一下,以后忘了也方便再捡起来. 首先先来解释一下什么是无参数RCE: 形式: if(';' === preg_replace('/[^\W] ...
- Vuex 是什么
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具 ...