原因:应客户需求,在系统中浏览附件内容,需要先下载到本地然后打开,对使用造成了不便,要求可以不需下载直接在浏览器中打开减少操作步骤。

领导给了3天时间,最后查找方法,写测试项目,往正式项目添加,测试,修bug,优化下来总共花费了大概两天多时间。下面给出解决经验,主要把遇到的坑

给说一下。

1.研究方案

参考:http://www.cnblogs.com/xuse/p/3710647.html

使用FlexPaper实现office文件的预览(C#版)
http://www.cnblogs.com/zzPrince/p/3378336.html

flexpaper使用:

http://www.cnblogs.com/Gnepner/archive/2011/08/19/2145493.html

通过研究发现,网上流传很多方法可以实现该需求,排除第三方控件的话,有两种比较流行,一种是把文档转化为swf格式,还有一种是转化为html实现在线预览。

但是按照网上的原话,转化为html方法很不科学,转换的文件格式丢失,仅限于IIS服务器,利用asp.net。配置麻烦,正如微软所说,读取office不是这么干的,鉴于此

先尝试另一种方案。

原理:先将office转换为PDF,再转换为SWF,最后通过网页加载Flash预览。

2.搭建测试项目

2.1工具:

a.安装Microsoft Office 2007以上版本,主要是需要用到里面四个类库,稍后列出。

b.Swftools

下载地址:http://www.swftools.org/download.html

这有个小坑,看好要下载window版。

c.flexpaper

下载地址:http://flexpaper.devaldi.com/download.htm

2.2搭建测试项目

由于本系统用的是Mvc开发,所以先建了一个MvcTest项目。

添加引用,此处有小坑,注意在引用的类库上右键,把嵌入互操作类型改为False,

引用版本最好12.0.0.0,14.0.0.0经测试也可用,网上说存在权限认证问题。

MVC 附件在线预览

添加工具,项目文件

MVC 附件在线预览

a.Common>Utils里面写Word,Excel,PPT等转化为PDF的方法,网上有很多,主要是Excel的转化经测试有一些有问题,贴一个靠谱的

 1  public static bool ExcelToPDF(string sourcePath, string targetPath)
 2         {    
 3             bool result = false;
 4             Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
 5             object missing = Type.Missing;
 6             Excel.ApplicationClass application = null;
 7             Excel.Workbook workBook = null;
 8             try
 9             {
10                 application = new Excel.ApplicationClass();
11                 object target = targetPath;
12                 object type = targetType;
13                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
14                         missing, missing, missing, missing, missing, missing, missing, missing, missing);
15 
16                 workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
17                 result = true;
18             }
19             catch
20             {
21                 result = false;
22             }
23             finally
24             {
25                 if (workBook != null)
26                 {
27                     workBook.Close(true, missing, missing);
28                     workBook = null;
29                 }
30                 if (application != null)
31                 {
32                     application.Quit();
33                     application = null;
34                 }
35                 GC.Collect();
36                 GC.WaitForPendingFinalizers();
37                 GC.Collect();
38                 GC.WaitForPendingFinalizers();
39             }
40             return result;
41            
42         }
ExcelToPDF

相关文章:

  • 2021-08-08
  • 2021-11-12
  • 2021-12-14
  • 2021-10-09
  • 2021-12-16
  • 2021-09-17
  • 2021-08-01
  • 2021-12-08
猜你喜欢
  • 2021-12-14
  • 2021-12-08
  • 2021-09-08
  • 2021-09-12
  • 2021-11-02
  • 2021-10-19
  • 2021-12-08
  • 2021-11-14
相关资源
相似解决方案