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文档的预览 在博客园很多文章 ...
随机推荐
- Win7微信DLL劫持反弹SHELL(10.9 第十七天)
(该文参考自网络其他人资料,仅为学习,不得用于非法用途) 准备的工具:kali虚拟机 W7虚拟机 微信 ProcessExplorer the-backdoor-factory-master 打开微信 ...
- redis性能测试方法
redis本身设计为单线程服务器,性能本身并不随着多核而提高,但是会随着cpu本身而改变,AMD的可能只有Intel一半的性能,Intel是最好的选择. 性能会随着连接数的增多而下降,30000大概只 ...
- 数据结构顺序表中Sqlist *L,&L,Sqlist *&L
//定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...
- 一条命令解决:No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
1.找到目录D:\android\Sdk\ndk-bundle\toolchains.(根据自己的安装路径找到) 2.该路径下打开终端执行ln -sf aarch64-linux-android-4. ...
- kettle将csv文件导入数据库
具体过程学习了: 1.连接数据库 2.添加新资源库 3.选择Other Repositories 4.选择Database Repository,第二个需要配置额外参数 5.连接数据库相关设置 6.连 ...
- Canvas绘制水波进度加载
效果: 用到图片下载: 自定义View: package com.czm.mysinkingview; import android.content.Context; import android.g ...
- Beyond Compare 文件对比工具的使用
Beyond Compare 文件对比工具的使用 Beyond Compare 工具下载地址: http://www.onlinedown.net/soft/633850.htm 本文下载地址:E:\ ...
- ODBC OLEDB
ODBC OLEDB https://www.cnblogs.com/dachuang/p/8615754.html
- NoSQL:
NoSQL:NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关系数据 ...
- sendmail 的安装、配置与发送邮件的具体实现
Ubuntu 中sendmail 的安装.配置与发送邮件的具体实现 centos安装sendmail与使用详解 CentOS下搭建Sendmail邮件服务器 使用外部SMTP发送邮件 使用mailx ...