c#预览office文档最全面的例子

前段时间项目有一个在线预览office文档的需求,做了一个小栗子,和大家一起学习学习!如何实现这个功能呢?我总结了几个方案:

方案一:将需要转换的文档通过office组件转换成html查看

方案二:将需要转换的文档通过office组件转换成pdf查看

方案三:通过在服务器部署微软的office web apps进行文档预览 参考网页:
https://www.cnblogs.com/yanweidie/p/4516164.html

方案四:office 在线预览服务

方案五:使用网上很多收费或者不收费的第三方插件

我使用的是方案一,也试过方案二和方案三,相比而言,转换成html比转换成pdf的效率要快很多,只是页面没有pdf美观,而在服务器部署office web apps配置失败了,所以选择放弃。

以下是方案一的案例:

1.首先引入com组件中office库,然后在程序集扩展中引入word的dll,我本来是想使用.net core做这个在线预览offce文档,结果发现.net core 并没有office库,可能是我没有找到

值得注意的是,虽然项目里引入office库,但运行项目后,发现会遇到各种问题,原因是office库并没有你电脑的office应用匹配,所以需要在电脑需要部署office插件

1.行栏中输入命令:dcomcnfg,打开组件服务管理窗口,组件服务->我的电脑->DCOM配置
2.找到Microsoft Excel Application 设置属性标识:交互式用户,在安全了的三个配置全选自定义配置,添加Everyone 组,给这个组的权限都勾选未允许,
3.再找到 Microsoft PowerPoint 幻灯片 权限设置和第二步一样
4.再找到Microsoft Word 97 - 2003 文档 权限设置和第二步一样 (可能office版本不同)
5.在项目的webconfig 里 <system.web> 下 添加 , userName、password为服务器的对应账号密码

查了很多资料,亲测可用 参考网页:https://www.cnblogs.com/5426z/articles/4865312.html

2.预览Excel

        /// 预览Excel
        /// </summary>
        /// <param name="physicalPath">解码的地址</param>
        /// <param name="url"></param>
        public string PreviewExcel(string physicalPath, string url)
        {
   
            var resultUrl = string.Empty;
            var serverUrl = string.Empty;
            try
            {
   
                Microsoft.Office.Interop.Excel.Application application = null;
                //参数
                Workbook workbook = null;
                application = new Microsoft.Office.Interop.Excel.Application();
                object missing = Type.Missing;
                object trueObject = true;
                application.Visible = false;
                application.DisplayAlerts = false;
                workbook = application.Workbooks.Open(physicalPath, missing, trueObject, missing, missing, missing,
                   missing, missing, missing, missing, missing, missing, missing, missing, missing);
                object format = XlFileFormat.xlHtml;
                string htmlName = Path.GetFileNameWithoutExtension(physicalPath) + ".html";
                String outputFile = Path.GetDirectoryName(physicalPath) + "\\" + htmlName;
                workbook.SaveAs(outputFile, XlFileFormat.xlHtml, null, null, false, false,
                   XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);

                workbook.Close();
                application.Quit();
                resultUrl = Path.GetDirectoryName(url) + "\\" + htmlName;
                var lastUrl = Path.GetFileName(resultUrl);
                resultUrl = ConfigurationManager.AppSettings["ResultUrl"] + lastUrl;
                serverUrl = ConfigurationManager.AppSettings["ServerUrl"];
            }
            catch (Exception ex)
            {
   
                this.Write(ex);
            }
            return serverUrl + resultUrl;
}

4.预览Word

>         /// 预览Word(HtmL类型)
>         /// </summary>
>         /// <param name="physicalPath"></param>
>         /// <param name="url"></param>
>         /// <returns></returns>
>         public string PreviewWord(string physicalPath, string url)
>         {
   
> 
>             var resultUrl = string.Empty;
>             var serverUrl = string.Empty;
>             object outputFile1 = new object();
>             object missing1 = new object();
>             //检查doc 是否被实例化
>             var idd = 0;
>             var ss = 1;
>             try
>             {
   
>                 Microsoft.Office.Interop.Word._Application application = null;
>                 Microsoft.Office.Interop.Word._Document doc = null;
>                 application 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值