[Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!

引言

之前项目需要,查找了office文档在线预览的解决方案,顺便记录一下,方便以后查询。

方案一

直接在浏览器中打开Office文档在页面上的链接。会弹出如下窗口:

 

优点:主流浏览器都支持。

缺点:Office文档链接在浏览器中打开,会有如上图的提示,需用户自己选择打开或者保存功能,如果客户电脑上安装迅雷下载软件,会启动迅雷下载,用户体验不好。

方案二

office文档转html,首先引入com组件中office库,然后在程序集扩展中引入word,excel,ppt的dll。

 

然后F6生成,会报如下错误:

解决办法:

office文档转换html辅助类:

  Office2HtmlHelper
  Office2Html.aspx
复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace Wolfy.OfficePreview
 9 {
10     public partial class Office2Html : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14 
15         }
16         protected void btnWord_Click(object sender, EventArgs e)
17         {
18             Button btn = sender as Button;
19             switch (btn.CommandArgument)
20             {
21                 case "docx":
22                     Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/Html/"), "分析某网站的SEO策略(外链篇)");
23                     break;
24                 case "xlsx":
25                     Office2HtmlHelper.Excel2Html(MapPath("/Excel/1994-2013北京市历年最低工资标准.xlsx"), MapPath("/Html/"), "1994-2013北京市历年最低工资标准");
26                     break;
27                 case "ppt":
28                     Office2HtmlHelper.PPT2Html(MapPath("/PPT/23种设计模式详解.ppt"), MapPath("/Html/"), "23种设计模式详解");
29                     break;
30                 default:
31                     break;
32             }
33         }
34     }

35 }
复制代码

测试结果:

这里为了测试特找了含有图片的office文档,浏览正常:

 要求:机器需安装office,并且office环境是纯净的,所谓纯净就是不能有多个版本,lz曾经在电脑上安装过wps,被害苦了总是报如下错误:

报这个错误,只能哭了,网上的关于00046的解决办法都尝试了,不行。然后不得不重新安装office,然后笑了。最好安装office完整版,因为原来装的不是完整版,不知道有没有这方面的原因,也没有测试,建议完整版。

方案三

office文档转PDF,PDF转swf,使用flexpaper+swftools实现在线浏览。

在操作office2007时,需安装SaveAsPDFandXPS.exe ,安装成功后,如图所示:

只有安装了SaveAsPDFandXPS.exe,程序操作office文档,才有office文档另存为pdf文件。office2010不需要安装了,内置有这个功能。

 核心代码:

  Office2PDFHelper
  Office2PDF
  Office2PDF.aspx

测试结果:

 

此方案office转pdf文件的过程的要求与方案二要求相同。

pdf转换完成后,就可以将pdf转换为swf,使用flexpaper+swftools实现在线浏览了,可参考我之前的一篇文章:

FlexPaper+SWFTool+操作类=在线预览PDF

方案四

office文档直接转换为swf,使用flexpaper+swftool实现在先浏览。

office直接转换为swf,这里使用flashpaper来实现:

FlashPaper是一个虚拟打印机,可将word文件直接转化成swf格式文件(.doc.xls .txt .pdf等文件都可以正常生成SWF格式)

这里只贴出核心代码:

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 
 9 namespace Wolfy.OfficePreview
10 {
11     public partial class Office2Swf : System.Web.UI.Page
12     {
13         protected void Page_Load(object sender, EventArgs e)
14         {
15 
16         }
17         protected void btnWord_Click(object sender, EventArgs e)
18         {
19             Button btn = sender as Button;
20             switch (btn.CommandArgument)
21             {
22                 case "docx":
23                     ConvertOffice2Swf(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/SWF/分析某网站的SEO策略(外链篇).swf"));
24                     break;
25                 case "xlsx":
26                     Office2PDFHelper.XLSConvertToPDF(MapPath("/Excel/1994-2013北京市历年最低工资标准.xlsx"), MapPath("/SWF/1994-2013北京市历年最低工资标准.swf"));
27                     break;
28                 case "ppt":
29                     Office2PDFHelper.PPTConvertToPDF(MapPath("/PPT/23种设计模式详解.ppt"), MapPath("/SWF/23种设计模式详解.swf"));
30                     break;
31                 default:
32                     break;
33             }
34         }
35         /// <summary>
36         /// office 转swf
37         /// </summary>
38         /// <param name="officePath">要转换的office文档路径</param>
39         /// <param name="swfPath">转换后swf的路径</param>
40         private void ConvertOffice2Swf(string officePath, string swfPath)
41         {
42             Process process = new Process();     //创建进程对象 
43             ProcessStartInfo startInfo = new ProcessStartInfo();
44             string paperroot = @"C:\Program Files\Macromedia\FlashPaper 2\FlashPrinter.exe";//这里是FlashPrinter的路径
45             string docFile = officePath;
46             string swfFile = swfPath;
47             startInfo.FileName = paperroot;
48             startInfo.Arguments = docFile + " -o " + swfFile;
49             startInfo.UseShellExecute = false;     //不使用系统外壳程序启动 
50             startInfo.RedirectStandardInput = false;   //不重定向输入 
51             startInfo.RedirectStandardOutput = false;   //重定向输出 
52             startInfo.CreateNoWindow = true;     //不创建窗口 
53             process.StartInfo = startInfo;
54             process.Start();   
55             if (process != null)
56                 process.Close();
57            
58         }
59     }
60 }
复制代码

鉴于测试时,flashpaper在将office文档转换为swf的时候,在使用flexpaper的浏览时,出现转换的内容为空,猜测:flexpaper能打开的swf文件与flashpaper转的swf文件不兼容。最后使用flashpaper将office文档转换为pdf,然后走方案三,pdf转swf的步骤。另外本地测试时,没问题。将项目部署在IIS上,不能浏览,出现卡死的情况,调试发现,文件太大,在office还没完全转换为pdf的情况下,swftool工具就去寻找pdf文件,出现错误。

IIS上,无法浏览,查询网上解决方案,和权限这块有关,按照步骤设置了,未果,有点遗憾。

方案五

使用点聚公司的weboffice控件,测试后发现兼容性较差,放弃。有兴趣的可以研究一下。

方案六

office转pdf后,直接浏览器打开,此方案鉴于目前主流浏览器都集成adobe reader功能,可实现直接打开PDF文件。将pdf文件链接可直接打开。

必要条件:本地需安装adobe reader类似软件。

总结

鉴于项目情况选择一个适合的方案,其中有方案只是曲线救国,但是同样能达到要求。如果您觉得对你有所帮助,不妨推荐一下,让更多的人都能看到,谢谢你能看到文章最后。

参考文章:

http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html

http://www.cnblogs.com/lexlin/articles/2478027.html

http://www.cnblogs.com/gossip/p/3473024.html

http://www.cnblogs.com/expectszc/archive/2012/04/04/2432149.html

img作者:Wolfy
出处:http://www.cnblogs.com/wolf-sun/
本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。愿与志同道合的朋友一起成长......
技术交流群:329276418
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
【核心代码】 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 using Aspose.Cells; using Aspose.Slides.Pptx; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DocOnlineView.UI.Controllers.MVCAPI { public class HomeController : ApiController { [HttpGet] public DataTable CourseViewOnLine(string fileName) { DataTable dtlist = new DataTable(); dtlist.Columns.Add("TempDocHtml", typeof(string)); string fileDire = "/Files"; string sourceDoc = Path.Combine(fileDire, fileName); string saveDoc = ""; string docExtendName = System.IO.Path.GetExtension(sourceDoc).ToLower(); bool result = false; if (docExtendName == ".pdf") { //pdf模板文件 string tempFile = Path.Combine(fileDire, "temppdf.html"); saveDoc = Path.Combine(fileDire, "viewFiles/onlinepdf.html"); result = PdfToHtml( sourceDoc, System.Web.HttpContext.Current.Server.MapPath(tempFile), System.Web.HttpContext.Current.Server.MapPath(saveDoc)); } else { saveDoc = Path.Combine(fileDire, "viewFiles/onlineview.html"); result = OfficeDocumentToHtml( System.Web.HttpContext.Current.Server.MapPath(sourceDoc), System.Web.HttpContext.Current.Server.MapPath(saveDoc)); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值