最近在做了一个项目,要求是对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)的更多相关文章

  1. OFFICE 文档转换为html在线预览

    OFFICE 文档在线预览方案很多: 服务器先转换为PDF,再转换为SWF,最后通过网页加载Flash预览,比如flexpaper Office文档直接转换为SWF,通过网页加载Flash预览 微软的 ...

  2. java将office文档pdf文档转换成swf文件在线预览

    第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...

  3. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  4. 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

    条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上)     实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...

  5. [Office Web Apps]实现在线office文档预览

    摘要 在使用office web apps实现office文档在线预览的时候,需要注意的地方. web api web api作为owa在线预览服务回调的接口,这里面核心代码片段如下: using H ...

  6. 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...

  7. [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...

  8. Java版office文档在线预览

    java将office文档pdf文档转换成swf文件在线预览 第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux ...

  9. (转)基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览

    http://www.cnblogs.com/wuhuacong/p/3871991.html 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览 在博客园很多文章 ...

随机推荐

  1. Tomcat JDK MySql 安装配置

    Tomcat 7绿色版指定jdk并注册服务  https://blog.csdn.net/weixin_43976019/article/details/89386171   例如:service.b ...

  2. P 1026 程序运行时间

    转跳点:

  3. Fedora 32大变化:将删除Python 2及其软件包

    导读 虽然Fedora 30还没有上市,Fedora 32直到大约一年后才上市,但我们已经知道一个很大的变化:删除Python 2和包依赖它.随着Fedora 32将于2020年上半年推出,超过了Py ...

  4. CGridCtrl显示图片

  5. windows操作

    5.windows激活 数字权利许可工具激活 https://jingyan.baidu.com/article/9113f81b4d49232b3314c75e.html 4.网络连接不上 原因,v ...

  6. 【LeetCode】解数独

    做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...

  7. 调用servlet报The requested resource is not available.

    调用servlet的时候经常有这种报错,一般来说我直到现在遇到的情况大致有以下几类: 1.参数写错了 在新创建的servlet文件中有这么一行代码,“/LoginCheck”这个一定要和form表单中 ...

  8. 创建了以个vagrant box centos php7 nginx swoole git

    php7.2.9 centos7  nginx.1.16  swoole4.4.4 下载地址 链接:https://pan.baidu.com/s/14p7xIa0ZZigRuYvZxnMsYA 提取 ...

  9. CSU2004:Finding words(含指定不相交前后缀的模式串计数)

    题:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2004 题意:给定n个模式串,m个询问,每个询问是“前缀+‘*’+后缀 ”的组合的串S,输出 ...

  10. XML--XML Schema Definition(三)

    参考 http://www.w3school.com.cn/schema/index.asp XSD 复合元素 复合元素指包含其他元素及/或属性的 XML 元素. 有四种类型的复合元素: 空元素 包含 ...