当前位置:首页 » 《我的小黑屋》 » 正文

asp.net core调用wps实现word转pdf的方法

25 人参与  2024年09月23日 11:21  分类 : 《我的小黑屋》  评论

点击全文阅读


1,首先安装wps,从官网下载安装包

2,创建.net core控制项目

添加com引用,搜索wps

 

 

准备一个word文档,名字叫001.docx,随便编写一些文字内容

3,word转pdf

编写代码

namespace WPSStu01{    internal class Program    {        static void Main(string[] args)        {            Console.WriteLine("转化开始");            var inputFile = "001.docx";            var outputFile = "001.pdf";            WordExportAsPdf(inputFile, outputFile);            Console.WriteLine("转化成功");            Console.ReadKey();        }        /// <summary>        /// 转换为pdf文件,适合(.doc、.docx、.mht、.htm文件类型)        /// </summary>        /// <param name="fileName"></param>        /// <param name="outputFileName"></param>        /// <returns></returns>        public static string WordExportAsPdf(string fileName, string outputFileName)        {            string isSucceed = "OK";            Word.WdExportFormat fileFormat = Word.WdExportFormat.wdExportFormatPDF;            Word.Application wordApp = null;            if (wordApp == null) wordApp = new Word.Application();            Word._Document wordDoc = null;            try            {                wordDoc = wordApp.Documents.Open(fileName, false, true);                wordDoc.ExportAsFixedFormat(outputFileName, fileFormat);            }            catch (Exception ex)            {                isSucceed = ex.Message;            }            finally            {                if (wordDoc != null)                {                    wordDoc.Close(false);                    wordDoc = null;                }                if (wordApp != null)                {                    wordApp.Quit(false);                    wordApp = null;                }            }            return isSucceed;        }    }}

 如果启动项目报错

 要选择一下32位程序

如果发现还是不行,最后换成.net framework 4.8的控制台项目
添加dll的引用,dll需要去安装的wps里面查找 

 

Console.WriteLine("转化开始");var exePath = System.AppDomain.CurrentDomain.BaseDirectory;var inputFile = Path.Combine(exePath, "001.docx");var outputFile = Path.Combine(exePath, "001.pdf");WordExportAsPdf(inputFile, outputFile);Console.WriteLine("转化成功");Console.ReadKey();

 

asp.net core也可以问题根本原因是路径的问题,不能些相对路径,必须绝对路径 

 

4,excel转pdf 

/// <summary>/// Excel转换为pdf文件/// </summary>/// <param name="fileName"></param>/// <param name="outputFileName"></param>/// <returns></returns>public static string ExcelExportAsPdf(string fileName, string outputFileName){    string isSucceed = "OK";    Excel.Application excelApp = null;    if (excelApp == null)        excelApp = new Excel.Application();    Excel.Workbook workBook = null;    try    {        workBook = excelApp.Workbooks.Open(fileName, false, true);        workBook.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF,outputFileName);    }    catch (Exception ex)    {        isSucceed = ex.Message;    }    finally    {        if (workBook != null)        {            workBook.Close(false);            workBook = null;        }        if (excelApp != null)        {            excelApp.Quit();            excelApp = null;        }    }    return isSucceed;}

 调用

Console.WriteLine("转化开始");var exePath = System.AppDomain.CurrentDomain.BaseDirectory;var inputFile = Path.Combine(exePath, "002.xls");var outputFile = Path.Combine(exePath, "002.pdf");ExcelExportAsPdf(inputFile, outputFile);Console.WriteLine("转化成功");Console.ReadKey();

ppt转pdf

/// <summary>/// PPT转换为pdf文件/// </summary>/// <param name="fileName"></param>/// <param name="outputFileName"></param>/// <returns></returns>public static string PptExportAsPdf(string fileName, string outputFileName){    string isSucceed = "OK";    PowerPoint.Application pptApp = null;    if (pptApp == null)        pptApp = new PowerPoint.Application();    PowerPoint.Presentation presentation = null;    try    {        presentation = pptApp.Presentations.Open(fileName);        presentation.ExportAsFixedFormat(outputFileName,PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);    }    catch (Exception ex)    {        isSucceed = ex.Message;    }    finally    {        if (pptApp != null)        {            presentation.Close();            pptApp = null;        }        if (pptApp != null)        {            pptApp.Quit();            pptApp = null;        }    }    return isSucceed;}

调用

Console.WriteLine("转化开始");var exePath = System.AppDomain.CurrentDomain.BaseDirectory;var inputFile = Path.Combine(exePath, "003.pptx");var outputFile = Path.Combine(exePath, "003.pdf");PptExportAsPdf(inputFile, outputFile);Console.WriteLine("转化成功");Console.ReadKey();


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/163346.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 贵妃每天只想当咸鱼小说(萧兮兮洛清寒)(贵妃每天只想当咸鱼)整本+后续+结局在线阅读
  • 云纾君迟玉:+后续+番外半溪明月枕清风无删减小说在线无广告高口碑小说
  • 新章速递既负如来又负卿是什么小说(莫司淮唐乐薇)完本阅读无广告(莫司淮唐乐薇)
  • 林齐(林齐)火爆小说全集免费阅读_反转传奇林齐:结局+番外无弹窗最新章节笔趣阁(林齐)
  • 萧兮兮洛清寒小说(贵妃每天只想当咸鱼)起点章节+全篇阅读热门作品预订
  • 穿成塌房顶流,我靠爆料翻红(陈昭陈莹静)_穿成塌房顶流,我靠爆料翻红
  • (番外)+(全文)谢青霄林相宜(八零奉子成婚,死对头成了妻管严:全文+结局+番外)全文免费阅读无弹窗大结局_(谢青霄林相宜)最新章节列表_笔趣阁(八零奉子成婚,死对头成了妻管严:全文+结局+番外)
  • 傅修言沈知穗小说(沈知穗傅修言)小说***下载_章节前文+后续(傅修言沈知穗)
  • 陆译林初夏小说(替身攻略失败后摆烂了)全文免费阅读_(替身攻略失败后摆烂了)陆译林初夏小说最新章节列表
  • 全书浏览说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)_说好摸鱼打游戏,你爆杀华尔街?(王文斌方幻)全书结局
  • (重生后高傲妻子我不爱了)重生后高傲妻子我不爱了(梁言彻尹暖芸)无套路无弹窗全部章节列表
  • (番外)+(全文)傅修言沈知穗:番外+全文+后续(沈知穗傅修言)完整版小说阅读_傅修言沈知穗:番外+全文+后续免费阅读_笔趣阁(沈知穗傅修言)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1