欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net core調用wps實現(xiàn)word轉pdf的過程

 更新時間:2024年08月16日 09:51:56   作者:假裝我不帥  
這篇文章主要介紹了asp.net core調用wps實現(xiàn)word轉pdf的過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

安裝wps

https://www.wps.cn/

創(chuàng)建.net core控制項目

添加com引用,搜索wps

準備word,名字叫001.docx

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位程序

發(fā)現(xiàn)還是不行,最后換成.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也可以問題根本原因是路徑的問題,不能些相對路徑,必須絕對路徑

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();

到此這篇關于asp.net core 調用wps實現(xiàn)word轉pdf的文章就介紹到這了,更多相關asp.net core word轉pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • asp.net中倒計時自動跳轉頁面的實現(xiàn)方法(使用javascript)

    asp.net中倒計時自動跳轉頁面的實現(xiàn)方法(使用javascript)

    本篇文章介紹了,asp.net中倒計時自動跳轉頁面的實現(xiàn)方法(使用javascript)。需要的朋友參考下
    2013-05-05
  • Visual Studio 2017新版發(fā)布 更強大!

    Visual Studio 2017新版發(fā)布 更強大!

    Visual Studio 2017新版發(fā)布 更強大!對Visual Studio 2017感興趣的小伙伴們可以參考一下
    2017-05-05
  • visual studio 2012安裝配置方法圖文教程 附opencv配置教程

    visual studio 2012安裝配置方法圖文教程 附opencv配置教程

    這篇文章主要為大家分享了visual studio 2012安裝配置方法圖文教程,文中附opencv配置教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 更改.NET中的默認時區(qū)的操作方法

    更改.NET中的默認時區(qū)的操作方法

    除了"在操作系統(tǒng)中修改時區(qū)信息,然后重啟.NET應用程序,使其生效"之外,如何在不修改操作系統(tǒng)時區(qū)的前提下,修改.NET中的默認時區(qū)呢,下面小編給大家分享更改.NET中的默認時區(qū)的操作方法,感興趣的朋友一起看看吧
    2024-06-06
  • .NET發(fā)布網(wǎng)站詳細步驟

    .NET發(fā)布網(wǎng)站詳細步驟

    這篇文章主要為大家介紹了.NET發(fā)布網(wǎng)站詳細步驟,包括web網(wǎng)站發(fā)布、IIS6 安裝方法、ASP.NET v4.0 安裝方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • asp.net Mvc4 使用ajax結合分頁插件實現(xiàn)無刷新分頁

    asp.net Mvc4 使用ajax結合分頁插件實現(xiàn)無刷新分頁

    本篇文章主要介紹了 asp.net Mvc4 使用ajax結合分頁插件實現(xiàn)無刷新分頁,ajax通過回調函數(shù)把控制器返回的分部視圖內(nèi)容加載到主視圖中顯示,有興趣的可以了解一下。
    2017-01-01
  • 淺談ASP.NET Core 2.0 部分視圖(譯)

    淺談ASP.NET Core 2.0 部分視圖(譯)

    本篇文章主要介紹了淺談ASP.NET Core 2.0 部分視圖(譯),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • VS2022?.NET5一鍵發(fā)布到遠程騰訊云IIS服務器的詳細步驟

    VS2022?.NET5一鍵發(fā)布到遠程騰訊云IIS服務器的詳細步驟

    這篇文章主要介紹了VS2022?.NET5一鍵發(fā)布到遠程騰訊云IIS服務器,首先需要添加服務器相關功能,文中通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • ASP.NET Core應用中與第三方IoC/DI框架的整合

    ASP.NET Core應用中與第三方IoC/DI框架的整合

    ASP.NET Core應用中,針對第三方DI框架的整合可以通過在定義Startup類型的ConfigureServices方法返回一個ServiceProvider來實現(xiàn)。但是并不是那么容易的,下面通過實例給大家分享一下
    2017-04-04
  • ASP.NET WebForms實現(xiàn)全局異常捕獲與處理的最佳實踐

    ASP.NET WebForms實現(xiàn)全局異常捕獲與處理的最佳實踐

    文章介紹了在ASP.NET WebForms中實現(xiàn)全局異常捕獲與處理的最佳實踐,包括在Global.asax中使用Application_Error、在Web.config中配置customErrors、在代碼中使用try-catch、全局異常過濾以及使用日志記錄庫等方法,感興趣的朋友一起看看吧
    2025-01-01

最新評論