.NetCore——PPT、EXCEL、WORD在线预览

.NetCore——PPT、EXCEL、WORD在线预览

一、介绍

近期在项目开发过程中遇到了PPT在线预览的问题,之前一直使用微软提供的预览接口 https://view.officeapps.live.com/op/view.aspx?src=(这里放要展示的外网可访问文件地址)。经过使用发现如果文件过大或者网络不稳定的时候就会经常性发生打开文件失败的问题。
一些在线预览、编辑的服务收费都比较昂贵,免费的支持的文件很小,要么就是带水印。所以最后采用了微软的组件将PPT转成图片来预览,编辑就算了。
优点:
1、转换速度块
2、操作简单
3、免费无限制(目前我的使用没遇到什么限制)

缺点
1、依赖office组件
2、linux没找到部署方式

二、使用

创建.NET6.0项目
安装NuGet包
我最终只是使用了Word、PPT的转换
将PPT转图片,Word转PDF
其它的需求可以自行百度解决
在这里插入图片描述
创建 OfficeConversion
添加方法 PPTConvertToPDF 将PPT转为JPG 代码如下

        ///<summary>        
        /// 把PowerPoint文件转换成格式文件       
        ///</summary>        
        ///<param name="sourcePath">源文件路径</param>     
        ///<param name="targetPath">目标文件路径</param> 
        ///<returns>true=转换成功</returns> 
        public async static Task<Response> PPTConvertToJPG(string sourcePath, string targetPath)
        {
            Response res = new();

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            /*
             *此处注意:要将源文件转换为什么格式
             *如果是示例中的ppSaveAsJPG将ppt转为jpg
             *那么要在下边persentation.SaveAs()中保存目标文件路径的后边加上.jpg的后缀
             *PpSaveAsFileType此处类型有很多,可以自行研究
             */
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsJPG;

            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetPath + ".jpg", targetFileType, MsoTriState.msoTrue);
            }
            catch (Exception ex)
            {
                res = new(0, null, ex.Message);
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            await Task.CompletedTask;
            return res;
        }

添加方法 DOCConvertToPDF 将Word转为PDF 代码如下

        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public async static Task<Response> DOCConvertToPDF(string sourcePath, string targetPath)
        {
            Response res = new();
            if (!Directory.Exists(targetPath))
                Directory.CreateDirectory(targetPath);
                
            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;
            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document wordDocument = null;
            try
            {
                Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                wordDocument = wordApplication.Documents.Open(
                    ref sourcePath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(targetPath + ".pdf",
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception ex)
            {
                res = new(0, null, ex.Message);
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            await Task.CompletedTask;
            return res;
        }

这样就完成了,Excel没什么要转的需求安装了NuGet包没有用到。
别的一些操作可自行查询
重点在部署后是否可以跑通,网上一些方法我尝试过后并不行,如果有需要后续将贴出部署时的配置。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值