.NET实现在网页中预览Office文件的3个方法

 更新时间:2021年9月22日 10:11  点击:1985

近日公司要搞一个日常的文档管理的东东,可以上传、下载各种文件,如果是office文件呢还必须得支持预览功能,其他的都好说但是唯独office预览功能比较麻烦,但是不能不做,废话不多说了一步步来吧。分析了下网易邮箱的文件预览功能,他用的是微软的组件,最早叫Office online,现在分开了叫Word online、Excel online ....等等,效果十分炫酷功能十分强大,但是查看了下对api的说明发现对服务器的要求比较苛刻而且配置比较复杂不太适合。然后 又看了下腾讯用的是永中第三方组件,效果嘛自然比不上微软的但是能用,综合网上的一些资料大概也就那么几种方式实现

1.使用Microsoft的Office组件将文件直接转换为html文件(优点:代码实现最简单,工作强度最小。缺点:效果极差)

2.使用Microsoft的Office组件将文件转换为PDF格式文件,然后再使用pdf2swf转换为swf文件,也就是flash文件在使用FlexPaper展示出来(优点:预览效果能接受,缺点:代码量大)

效果如图:

3. 使用Office online(优点:表现完美,缺点:不适合中小企业应用)

综合考虑决定使用第二种方法,经过次次波折终于可以使用,但是有个问题至今没有得到解决,调用Office组件的时候有时候会出现如下异常:

检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A),查阅无数资料还是不能解决,最让人不可接受的的是office文件必须标标准准毫无容错能力,当转换ppt文件时竟然会弹出转换进度框!!

好吧!那么我们改进它。

使用ASPOSE+pdf2swf+FlexPaper

关于ASPOSE大家可以到官网了解,这是款商业收费产品但是免费也可以使用

1、引用dll

2、编写转换帮助类

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Cells;
using Aspose.Words;
using Aspose.Slides;
using System.Text.RegularExpressions;
using System.IO;

namespace Souxuexiao.Common
{
    /// <summary>
    /// 第三方组件ASPOSE Office/WPS文件转换
    /// Writer:Helen Joe
    /// Date:2014-09-24
    /// </summary>
    public class AsposeUtils
    {
        /// <summary>
        /// PFD转换器位置
        /// </summary>
        private static string _EXEFILENAME = System.Web.HttpContext.Current != null
                ? System.Web.HttpContext.Current.Server.MapPath("/pdf2swf/pdf2swf.exe")
                : System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "\\pdf2swf\\pdf2swf.exe");

        #region 1.01 Wrod文档转换为PDF文件 +ConvertDocToPdF(string sourceFileName, string targetFileName)
        /// <summary>
        /// Wrod文档转换为PDF文件
        /// </summary>
        /// <param name="sourceFileName">需要转换的Word全路径</param>
        /// <param name="targetFileName">目标文件全路径</param>
        /// <returns>转换是否成功</returns>
        public static bool ConvertDocToPdF(string sourceFileName, string targetFileName)
        {
            Souxuexiao.API.Logger.error(string.Format("Wrod文档转换为PDF文件:sourceFileName={0},targetFileName={1}", sourceFileName, targetFileName));
            try
            {
                using (System.IO.Stream stream = new System.IO.FileStream(sourceFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    Document doc = new Document(sourceFileName);
                    doc.Save(targetFileName, Aspose.Words.SaveFormat.Pdf);
                }
            }
            catch (Exception ex)
            {
                Souxuexiao.API.Logger.error(string.Format("Wrod文档转换为PDF文件执行ConvertDocToPdF发生异常原因是:{0}",ex.Message));
            }
            return System.IO.File.Exists(targetFileName);
        }
        #endregion

        #region 1.02 Excel文件转换为HTML文件 +(string sourceFileName, string targetFileName, string guid)
        /// <summary>
        /// Excel文件转换为HTML文件
        /// </summary>
        /// <param name="sourceFileName">Excel文件路径</param>
        /// <param name="targetFileName">目标路径</param>
        /// <returns>转换是否成功</returns>
        public static bool ConvertExcelToHtml(string sourceFileName, string targetFileName)
        {
            Souxuexiao.API.Logger.info(string.Format("准备执行Excel文件转换为HTML文件,sourceFileName={0},targetFileName={1}",sourceFileName,targetFileName));
            try
            {
                using (System.IO.Stream stream = new System.IO.FileStream(sourceFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    Aspose.Cells.Workbook workbook = new Workbook(stream);
                    workbook.Save(targetFileName, Aspose.Cells.SaveFormat.Html);
                }
            }
            catch (Exception ex)
            {
                Souxuexiao.API.Logger.error(string.Format("Excel文件转换为HTML文件ConvertExcelToHtml异常原因是:{0}", ex.Message));
            }
            return System.IO.File.Exists(targetFileName);
        }
        #endregion

        #region 1.03 将PowerPoint文件转换为PDF +ConvertPowerPointToPdf(string sourceFileName, string targetFileName)
        /// <summary>
        /// 将PowerPoint文件转换为PDF
        /// </summary>
        /// <param name="sourceFileName">PPT/PPTX文件路径</param>
        /// <param name="targetFileName">目标文件路径</param>
        /// <returns>转换是否成功</returns>
        public static bool ConvertPowerPointToPdf(string sourceFileName, string targetFileName)
        {
            Souxuexiao.API.Logger.info(string.Format("准备执行PowerPoint转换PDF,sourceFileName={0},targetFileName={1}",sourceFileName,targetFileName));
            try
            {
                using (System.IO.Stream stream = new System.IO.FileStream(sourceFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                {
                    Aspose.Slides.Pptx.PresentationEx pptx = new Aspose.Slides.Pptx.PresentationEx(stream);
                    pptx.Save(targetFileName, Aspose.Slides.Export.SaveFormat.Pdf);
                }
            }
            catch (Exception ex)
            {
                Souxuexiao.API.Logger.error(string.Format("将PowerPoint文件转换为PDFConvertExcelToHtml异常原因是:{0}", ex.Message));
            }
            return System.IO.File.Exists(targetFileName);
        }
        #endregion

        #region 2.01 读取pdf文件的总页数 +GetPageCount(string pdf_filename)
        /// <summary>
        /// 读取pdf文件的总页数
        /// </summary>
        /// <param name="pdf_filename">pdf文件</param>
        /// <returns></returns>
        public static int GetPageCountByPDF(string pdf_filename)
        {
            int pageCount = 0;
            if (System.IO.File.Exists(pdf_filename))
            {
                try
                {
                    byte[] buffer = System.IO.File.ReadAllBytes(pdf_filename);
                    if (buffer != null && buffer.Length > 0)
                    {
                        pageCount = -1;
                        string pdfText = Encoding.Default.GetString(buffer);
                        Regex regex = new Regex(@"/Type\s*/Page[^s]");
                        MatchCollection conllection = regex.Matches(pdfText);
                        pageCount = conllection.Count;
                    }
                }
                catch (Exception ex)
                {
                    Souxuexiao.API.Logger.error(string.Format("读取pdf文件的总页数执行GetPageCountByPowerPoint函数发生异常原因是:{0}", ex.Message));
                }
            }
            return pageCount;
        }
        #endregion

        #region 2.02 转换PDF文件为SWF格式 +PDFConvertToSwf(string pdfPath, string swfPath, int page)
        /// <summary>
        /// 转换PDF文件为SWF格式
        /// </summary>
        /// <param name="pdfPath">PDF文件路径</param>
        /// <param name="swfPath">SWF生成目标文件路径</param>
        /// <param name="page">PDF页数</param>
        /// <returns>生成是否成功</returns>
        public static bool PDFConvertToSwf(string pdfPath, string swfPath, int page)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" \"" + pdfPath + "\"");
            sb.Append(" -o \"" + swfPath + "\"");
            sb.Append(" -z");
            //flash version
            sb.Append(" -s flashversion=9");
            //禁止PDF里面的链接
            sb.Append(" -s disablelinks");
            //PDF页数
            sb.Append(" -p " + "\"1" + "-" + page + "\"");
            //SWF中的图片质量
            sb.Append(" -j 100");
            string command = sb.ToString();
            System.Diagnostics.Process p = null;
            try
            {
                using (p = new System.Diagnostics.Process())
                {
                    p.StartInfo.FileName = _EXEFILENAME;
                    p.StartInfo.Arguments = command;
                    p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(_EXEFILENAME);
                    //不使用操作系统外壳程序 启动 线程
                    p.StartInfo.UseShellExecute = false;
                    //p.StartInfo.RedirectStandardInput = true;
                    //p.StartInfo.RedirectStandardOutput = true;

                    //把外部程序错误输出写到StandardError流中(pdf2swf.exe的所有输出信息,都为错误输出流,用 StandardOutput是捕获不到任何消息的...
                    p.StartInfo.RedirectStandardError = true;
                    //不创建进程窗口
                    p.StartInfo.CreateNoWindow = false;
                    //启动进程
                    p.Start();
                    //开始异步读取
                    p.BeginErrorReadLine();
                    //等待完成
                    p.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                Souxuexiao.API.Logger.error(string.Format("转换PDF文件为SWF格式执行PDFConvertToSwf函数发生异常原因是:{0}", ex.Message));
            }
            finally
            {
                if (p != null)
                {
                    //关闭进程
                    p.Close();
                    //释放资源
                    p.Dispose();
                }
            }
            return File.Exists(swfPath);
        }
        #endregion
    }
}
Office格式转换

3、将pdf文件转swf的转换器放到站点根目录下新建文件夹pdf2swf(我就是这么配置的,您随意)

4、配置FlexPaper

预览页面引用

 

复制代码 代码如下:

 <script src="/FlexPaper/js/swfobject.js" type="text/javascript"></script>
<script type="text/javascript" src="/FlexPaper/js/flexpaper_flash.js"></script>
 

控件容器以及设置项

复制代码 代码如下:

<div style="margin:0 auto;width:980px;">
            <div id="flashContent" style="display:none;">
                <p>
                    To view this page ensure that Adobe Flash Player version
                    10.0.0 or greater is installed.
                </p>
                <script type="text/javascript">
                    var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://");
                    document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>");
                </script>
            </div>
        <script type="text/javascript">
            var _filename = document.getElementById("_filename").value;
            var swfVersionStr = "9.0.0";
            var xiSwfUrlStr = "playerProductInstall.swf";
            var flashvars = {
                SwfFile: escape(_filename),
                Scale: 0.6,
                ZoomTransition: "easeOut",
                ZoomTime: 0.5,
                ZoomInterval: 0.1,
                FitPageOnLoad: false,
                FitWidthOnLoad: true,
                PrintEnabled: true,
                FullScreenAsMaxWindow: false,
                ProgressiveLoading: true,

                PrintToolsVisible: true,
                ViewModeToolsVisible: true,
                ZoomToolsVisible: true,
                FullScreenVisible: true,
                NavToolsVisible: true,
                CursorToolsVisible: true,
                SearchToolsVisible: true,
                SearchMatchAll:true,

                localeChain: "zh_CN"
            };
            var params = {
                quality: "high",
                bgcolor: "#ffffff",
                allowscriptaccess: "sameDomain",
                allowfullscreen: "true"
            }
            var attributes = { id: "FlexPaperViewer", name: "FlexPaperViewer" };
            swfobject.embedSWF("/FlexPaper/FlexPaperViewer.swf", "flashContent", "980", "620", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);
            swfobject.createCSS("#flashContent", "display:block;text-align:left;");
        </script>
        </div>

document.getElementById("_filename").value是预览文件的路径
 
OK  大功告成  ,至于如何上传,怎么保存上传的文件等等那些逻辑我这里就省略了。。。。,但是有个建议,当用户上传文件之后调用转换api生成预览文件是个耗时的操作,

文件越大耗时越长,也就是说生成预览文件的时候是需要时间的,因此我使用异步方式生成预览文件。

[!--infotagslink--]

相关文章

  • ASP.NET购物车实现过程详解

    这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • .NET Core下使用Kafka的方法步骤

    这篇文章主要介绍了.NET Core下使用Kafka的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 在ASP.NET 2.0中操作数据之七十二:调试存储过程

    在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
  • Win10 IIS 安装.net 4.5的方法

    这篇文章主要介绍了Win10 IIS 安装及.net 4.5及Win10安装IIS并配置ASP.NET 4.0的方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22
  • 详解.NET Core 3.0 里新的JSON API

    这篇文章主要介绍了详解.NET Core 3.0 里新的JSON API,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • php根据用户语言跳转相应网页

    当来访者浏览器语言是中文就进入中文版面,国外的用户默认浏览器不是中文的就跳转英文页面。 <&#63;php $lan = substr(&#8194;$HTTP_ACCEPT_LANGUAGE,0,5); if ($lan == "zh-cn") print("<meta http-equiv='refresh' c...2015-11-08
  • .net数据库操作框架SqlSugar的简单入门

    这篇文章主要介绍了.net数据库操作框架SqlSugar的简单入门,帮助大家更好的理解和学习使用.net技术,感兴趣的朋友可以了解下...2021-09-22
  • ASP.NET Core根据环境变量支持多个 appsettings.json配置文件

    这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • 记一次EFCore类型转换错误及解决方案

    这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
  • 腾讯视频怎么放到自己的网页上?

    腾讯视频怎么放到自己的网页上?这个问题是一个基本的问题,要把腾讯视频放到自己的网页有许多的办法,当然一般情况就是直接使用它们的网页代码了,如果你要下载资源再放到...2016-09-20
  • 基于JavaScript实现网页倒计时自动跳转代码

    这篇文章主要介绍了基于JavaScript实现网页倒计时自动跳转代码 的相关资料,需要的朋友可以参考下...2015-12-29
  • 网页头部声明lang=”zh-cn”、lang=“zh”、lang=“zh-cmn-Hans”区别

    我们现在使用的软件都会自动在前面加一个申明了,那么在网页头部声明lang=”zh-cn”、lang=“zh”、lang=“zh-cmn-Hans”区别是什么呢?下面我们就一起来看看吧. 单...2016-09-20
  • 详解ASP.NET Core 中基于工厂的中间件激活的实现方法

    这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22
  • C#使用Ado.Net更新和添加数据到Excel表格的方法

    这篇文章主要介绍了C#使用Ado.Net更新和添加数据到Excel表格的方法,较为详细的分析了OLEDB的原理与使用技巧,可实现较为方便的操作Excel数据,需要的朋友可以参考下...2020-06-25
  • .NET C#利用ZXing生成、识别二维码/条形码

    ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。这篇文章主要给大家介绍了.NET C#利用ZXing生成、识别二维码/条形码的方法,文中给出了详细的示例代码,有需要的朋友们可以参考借鉴。...2020-06-25
  • asp.net通过消息队列处理高并发请求(以抢小米手机为例)

    这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
  • ASP.NET单选按钮控件RadioButton常用属性和方法介绍

    RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22
  • ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表

    在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19
  • 详解.NET Core 使用HttpClient SSL请求出错的解决办法

    这篇文章主要介绍了.NET Core 使用HttpClient SSL请求出错的解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2021-09-22
  • Python调用.NET库的方法步骤

    这篇文章主要介绍了Python调用.NET库的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-05-09