引言

结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好。 提到Office文件在线预览,那么效果最好的应该就是百度文库的效果了,所以今天就忙里偷闲自己搞了下。

用到知识点

1、Office文件转化为Pdf文件。直接用.Net类库:Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Powerpoint、Microsoft.Office.Interop.Word、Office。 我本机装的office2013,所以我选择的是12.0的。

2、使用SwfTools将Pdf文件转化为Swf文件。

3、使用众所周知的FlexPaper浏览Swf文件(预览时有水印,不知道怎么去掉)。

Demo过程中遇到的问题

1、提示:"无法嵌入互操作类型Microsoft.Office.Interop.Word.ApplicationClass,请改用使用的接口"

解决:右键Dll,嵌入互操作类型改为false即可。

2、用到MsoTriState.msoTrue枚举类型参数时需要饮用Office.dll。 我一开始就没引用这个文件。

3、生成Swf文件时需要传入完整的路径,我一开始只传入了路径,没有swf文件名,试了几次没成功。

效果图

转化代码

public class OfficeHelper
{
/// <summary>
/// Word to Pdf
/// </summary>
/// <param name="srcFilePath"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public static bool WordToPdf(string srcFilePath, string targetFilePath)
{
bool rs = false;
Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Microsoft.Office.Interop.Word.ApplicationClass application = null; Microsoft.Office.Interop.Word.Document document = null; try
{
application = new Microsoft.Office.Interop.Word.ApplicationClass();
application.Visible = false;
document = application.Documents.Open(srcFilePath);
document.SaveAs();
document.ExportAsFixedFormat(targetFilePath, exportFormat); rs = true;
}
catch (Exception)
{
rs = false;
throw;
}
finally
{
if (document != null)
{
document.Close();
document = null;
} if (application != null)
{
application.Quit();
application = null;
} GC.Collect();
GC.WaitForPendingFinalizers();
} return rs;
} /// <summary>
/// Excel To Pdf
/// </summary>
/// <param name="srcFilePath"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public static bool ExcelToPdf(string srcFilePath, string targetFilePath)
{
bool rs = false;
Microsoft.Office.Interop.Excel.XlFixedFormatType exportFormat = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
Microsoft.Office.Interop.Excel.ApplicationClass application = null; Microsoft.Office.Interop.Excel.Workbook document = null; try
{
application = new Microsoft.Office.Interop.Excel.ApplicationClass();
application.Visible = false;
document = application.Workbooks.Open(srcFilePath);
document.SaveAs();
document.ExportAsFixedFormat(exportFormat, targetFilePath); rs = true;
}
catch (Exception)
{
rs = false;
throw;
}
finally
{
if (document != null)
{
document.Close();
document = null;
} if (application != null)
{
application.Quit();
application = null;
} GC.Collect();
GC.WaitForPendingFinalizers();
} return rs;
} /// <summary>
/// PPT To Pdf
/// </summary>
/// <param name="srcFilePath"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public static bool PptToPdf(string srcFilePath, string targetFilePath)
{
bool result;
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); persentation = application.Presentations.Open(srcFilePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetFilePath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
} /// <summary>
/// Pdf To Swf
/// </summary>
/// <param name="swfTools">Swf转化工具路径</param>
/// <param name="srcFilePath"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public static bool PdfToSwf(string toolsPath, string cmd)
{
bool iss = false;//判断是否转换成功,默认失败
try
{
using (Process p = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo(toolsPath, cmd);
p.StartInfo = psi;
p.Start();
p.WaitForExit();
iss = true;//转换成功
}
}
catch { }
return iss;
}
}
/// <summary>
/// Pdf文件转化为Swf
/// </summary>
/// <param name="swfTools">转化工具路径</param>
/// <param name="pdfPath">pdf文件目录</param>
/// <param name="pdfFileName">pdf文件名</param>
/// <param name="desPath">保存swf路径</param>
/// <returns></returns>
protected string PdfToSwf(string swfTools, string pdfPath, string pdfFileName, string desPath)
{
string fileFullName =Path.Combine(pdfPath,pdfFileName);
string fileFullNameWithoutEx = Path.GetFileNameWithoutExtension(pdfFileName);
string ext = Path.GetExtension(pdfFileName).ToLower(); string saveSwfPath = desPath + fileFullNameWithoutEx + ".swf";
string rs = fileFullNameWithoutEx + ".swf"; string cmdStr = " -t \"" + fileFullName + "\" -s flashversion=9 -o \"" + saveSwfPath + "\"";
bool iss = OfficeHelper.PdfToSwf(swfTools, cmdStr); return rs;
} /// <summary>
/// Office文件转pdf文件
/// </summary>
/// <param name="officePath">office文件保存路径</param>
/// <param name="officeFileName">office文件名</param>
/// <param name="pdfPath">保存pdf路径</param>
protected string OfficeToPdf(string officePath, string officeFileName, string pdfPath)
{
string fullPathName = Path.Combine(officePath, officeFileName);
string fileNameWithoutEx = Path.GetFileNameWithoutExtension(officeFileName);
string ext = Path.GetExtension(officeFileName).ToLower(); string savePdfPath = pdfPath + fileNameWithoutEx + ".pdf";
string retValue = fileNameWithoutEx + ".pdf"; switch (ext)
{
case ".doc":
OfficeHelper.WordToPdf(fullPathName, savePdfPath);
break;
case ".docx":
OfficeHelper.WordToPdf(fullPathName, savePdfPath);
break;
case ".xls":
OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
break;
case ".xlsx":
OfficeHelper.ExcelToPdf(fullPathName, savePdfPath);
break;
case ".ppt":
OfficeHelper.PptToPdf(fullPathName, savePdfPath);
break;
case ".pptx":
OfficeHelper.PptToPdf(fullPathName, savePdfPath);
break;
} return retValue;
}

参考

在Demo的过程中,学习和参考了两位博友的文章,在此表示感谢

Wolfy: http://www.cnblogs.com/wolf-sun/p/3569960.html

静以修身:http://www.cnblogs.com/zzPrince/p/3378336.html

源代码:http://yunpan.cn/csSPiPeVuum4s (提取码:4c7c)

在线预览Office文件【效果类似百度文库】的更多相关文章

  1. 在线预览Office文件【效果类似百度文库】(转载)

    转载地址:http://www.cnblogs.com/sword-successful/p/4031823.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前 ...

  2. 在线预览office文件

    Office Online 实现在线预览 office的在线预览,针对不同的浏览器版本和系统具有要求,具体的相关文档请参考官方文档. 利用office online 平台进行office 文档的在线查 ...

  3. 经管资源库项目总结----在线预览office文件的实现与总结

    依旧是这个经管的项目.在线预览作为资源和文档管理系统的一个很酷的并且是如此重要的功能,是必须要实现的.然后百度一下office在线预览,看起来so eazy啊,各种博客各种demo,一下子就做出效果来 ...

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

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

  5. 用pdf.js实现在移动端在线预览pdf文件

    用pdf.js实现在移动端在线预览pdf文件1.下载pdf.js    官网地址:https://mozilla.github.io/pdf.js/ 2.配置    下载下来的文件包,就是一个demo ...

  6. 使用pdfjs插件在线预览PDF文件

    前言 本文介绍在html中使用 pdfjs插件在线预览PDF文件的方法. 实现步骤 下载 pdfjs 并引入项目中 到PDFJS官网 http://mozilla.github.io/pdf.js/g ...

  7. asp.net在线预览txt文件(简单实现)

    最近在做文件的在线预览,发现txt文件没有一个较好的方法去实现,想了想可能是比较简单就直接在后台输出了 txt文件

  8. WinForm中预览Office文件

    WinForm预览Office文档 使用WinForm, WPF, Office组件 原理:使用Office COM组件将Word,Excel转换为XPS文档, 将WPF的DocumentViewer ...

  9. linux在线预览pdf文件开发思路

    准备:swftools,flexpaper 基本思路: 1,将pdf文件转化成swf文件 2,使用flexpaper预览swf文件 主要代码: 1,在linux中安装swftools.官网下载swft ...

随机推荐

  1. 【安装mysql】windows安装压缩版mysql5.7.15

      160915.1459 综述 中午安装成功,但是启动失败,很可能是压缩包有问题(从别人拷的),解决了近一个小时,没搞定,于是换条方案,重新去官网下载压缩包,这次看准了windows架构,且64-b ...

  2. tomcat报错java.lang.IllegalArgumentException: Document base XXXXX does not exist or is not a readable directory

    启动tomcat的时候报如下错误: java.lang.IllegalArgumentException: Document base F:\java\tools\tomcat\me-webapps\ ...

  3. ORACLE冷备份与恢复

    ORACLE备份和恢复有三种方式: (1)数据泵(expdp/impdp) (2)冷备份 (3)RMAN备份 就分类而言,(1)和(2)统有称为"冷"备份,(3)称为"热 ...

  4. UI控件(UISlider)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UISlider* slider = [[UISli ...

  5. AnguarJS 第二天----数据绑定

    Terms 今天学习AngularJS双向数据绑定的特性,这里面需要提到两个概念: 数据模型:数据模型是指 $scope对象, $scope对象是简单的javascript对象,视图可以访问其中的属性 ...

  6. [开源]C#二维码生成解析工具,可添加自定义Logo

    二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...

  7. dofile执行ANDROID APK里面的文件

    我使用dofile执行APK文件是不行的,比如 dofile("assets/res/flist")只能先拷贝到writablePath然后再dofile拿到数据后再清除这个临时文 ...

  8. Tomcat7基于Redis的Session共享实战一

    本文主要介绍如何使用redis对tomcat7的session进行托管. 1.安装Redisredis安装比较简单,此处略过. 2.配置两个Tomcat在本机上配置两个Tomcat,分别为tomcat ...

  9. 基于zipkin分布式链路追踪系统预研第一篇

    本文为博主原创文章,未经博主允许不得转载. 分布式服务追踪系统起源于Google的论文“Dapper, a Large-Scale Distributed Systems Tracing Infras ...

  10. vs code插件记录

    最近一段时间一直在迷恋vs code,使用了一段时间,发现它即精简又快速,安装插件又快又稳定而且插件说明也很详细,是一款继sublime后少得的良心前端编辑器,配合上一些插件可以补全一些不足以更趋向于 ...