Logo

Logo

Excel转PDF基于Microsoft.Office.Interop.Excel

Pastore Antonio
Pastore Antonio 2024年04月09日
884 阅读 0 评论 约 2003 字 阅读约 5 分钟

一.使用背景

我们在项目中使用的过程中很多时候需要转pdf,但是有很多控件不行,或者格式不对,所以很多时候只能使用Office的pdf
代码如下

二.源码

       /// <summary>
        /// Excel转PDF通过路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public string ConsultPDF(string filePath, string originName)
        {
            originName = originName.Replace(".xlsx", "");
            if (filePath.IndexOf(":", StringComparison.Ordinal) < 0) { filePath = DirFileHelper.GetMapPath("~/" + filePath); }
            string oldPath = filePath;
            string newPath = DirFileHelper.GetFilePath(filePath) + "\\" + originName + ".pdf";
            string result = "";
            ApplicationClass application = null;
            Workbook workBook = null;

            try
            {
                application = new ApplicationClass();
                workBook = application.Workbooks.Open(oldPath);
                workBook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, newPath, XlFixedFormatQuality.xlQualityStandard, true, false);

                result = newPath;
            }
            catch (Exception ex)
            {
                result = "false:" + ex.Message;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true);
                }
                if (application != null)
                {
                    application.Quit();
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            string serverPath = DirFileHelper.GetMapPath("~/");
            result = result.Replace(@"" + serverPath, "");
            return result;
        }

网上组件配置

打开comexp.msc -32

2、Microsoft Excel Application、和Microsoft Word 97-2003 Document属性里面进行配置,如下:
  标识:设为“交互式用户”
  安全:启动和激活权限添加“NETWORK SERVICE”,勾选本地启动和本地激活,访问权限添加类似

异常解决办法

当我们使用了这个插件之后也在网上使用了组件服务配置,但是发现如果在服务器上使用,有时候会掉线
其实如果要解决,实际上是我们组件服务配置错了在这里插入图片描述
游览选择管理员
在这里插入图片描述
最终输入密码即可
在这里插入图片描述

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它

附件下载

共 3 个文件
在这里插入图片描述
PNG 25.7 KB
在这里插入图片描述
PNG 37.6 KB
在这里插入图片描述
PNG 23.2 KB
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

探索AIGC相关的精彩内容,共 15 篇文章

Azure AI 服务之语音识别

简介 Azure AI 服务中的语音识别 API 是微软提供的一项先进技术,旨在帮助开发者轻松实现语 ... Excel转PDF基于Microsoft.Office.Interop.Excel

2026-02-17 · Xzavier Aaron
MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... Excel转PDF基于Microsoft.Office.Interop.Excel

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... Excel转PDF基于Microsoft.Office.Interop.Excel

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... Excel转PDF基于Microsoft.Office.Interop.Excel

2026-02-14 · Xzavier Aaron