zoukankan      html  css  js  c++  java
  • Microsoft Word、Excel、PowerPoint转Pdf

      Worksheet.ExportAsFixedFormat Method

    Mark:

      The ExportAsFixedFormat method is used to publish a workbook to either the PDF or XPS format.

    Syntax:

      expression .ExportAsFixedFormat(TypeFilenameQualityIncludeDocPropertiesIgnorePrintAreasFromToOpenAfterPublish)

      expression   A variable that represents a Workbook, Sheet, Chart, or Range object.

      

    Parameters:

       

     代码实现:

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
     
    // Office2Pdf.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"

    /*
        功能:Office文件格式(doc/docx、xls/xlsx、ppt/pptx)转PDF格式文件
        作者: ReOrganized by Michael Joessy 2017-06-01 Happy Children's Day
        使用前提
        [1]Office 2010(Word, Excel, PPT)
        编译环境:
        [1]VS2008
        [2]Win7 x64
        工程类型:Win32 Console Application
    */



    //导入Office类型库
    #pragma warning(disable:4786
    #import "C:Program Files (x86)Common Filesmicrosoft sharedOFFICE14MSO.DLL" 
        rename(
    "RGB","_OfficeRGB")         

    #import "C:Program Files (x86)Common Filesmicrosoft sharedVBAVBA6VBE6EXT.OLB" 
        rename(
    "Reference""ignorethis")



    #import "C:Program Files (x86)Microsoft OfficeOffice14MSWORD.OLB " 
        rename(
    "FindText","_FindText")
        rename(
    "Rectangle","_Rectangle")
        rename(
    "ExitWindows","_ExitWindows")


    #import "C:Program Files (x86)Microsoft OfficeOffice14MSPPT.OLB"
    #import "C:Program Files (x86)Microsoft OfficeOffice14EXCEL.EXE" 
        rename(
    "DialogBox","_DialogBox"
        rename(
    "RGB","_RGB"
        exclude(
    "IFont","IPicture")


    //使用头文件
    #include <string>
    #include <iostream>


    //声明
    int Word2Pdf(std::wstring inputFileName,std::wstring outputFileName);
    int Excel2Pdf(std::wstring inputFileName,std::wstring outputFileName);
    int Ppt2Pdf(std::wstring inputFileName,std::wstring outputFileName);

    //主函数
    int _tmain(int argc, _TCHAR* argv[])
    {
        
    int nRet = 0;
        HRESULT hr = CoInitialize(
    NULL);
        
    if (FAILED(hr))
        {
            std::cout << 
    "Initialize COM failed..." << std::endl;
            
    return 0;
        }

        std::wstring wsCmd;
        std::wstring wsS;
        std::wstring wsD;

        
    if(argc != 4)
        {
            std::cout << 
    "Command Usage: Office2pdf -[e|p|w] <source file name> <destination file name>" << std::endl;
            std::cout << 
    "         e.g.: Office2pdf -e MyFile.xlsx MyFile.pdf" << std::endl;
            
    return 1;
        }

        wsCmd = argv[
    1];
        wsS   = argv[
    2];
        wsD   = argv[
    3];

        
    if(wsCmd == L"-e")
        {
            nRet = Excel2Pdf(wsS.c_str(),wsD.c_str());
        }
        
    else if(wsCmd == L"-p")
        {
            nRet = Ppt2Pdf(wsS.c_str(),wsD.c_str());
        }
        
    else if(wsCmd == L"-w")
        {
            nRet = Word2Pdf(wsS.c_str(),wsD.c_str());
        }
     
        CoUninitialize(); 

        
    if(nRet != 0)
        {
            std::cout << 
    "Error:" << nRet << std::endl;
        }

        
    return nRet;
    }



    //实现
    int Word2Pdf(std::wstring inputFileName,std::wstring outputFileName)  
    {
        
    int nRet = 0;
        Word::_ApplicationPtr  pWordApp = 
    NULL
        Word::_DocumentPtr   pDoc = 
    NULL
        HRESULT hr = S_OK;

        BSTR szBstrOutputFileName;
        szBstrOutputFileName = SysAllocString(outputFileName.c_str());    
        hr = pWordApp.CreateInstance(
    __uuidof(Word::Application)); 
        
    if(hr != S_OK)
        {
            
    return 1;
        }

        Word::DocumentsPtr pDocs = 
    NULL
        pWordApp->get_Documents(&pDocs);

        
    if(pDocs==NULL)
        {
            nRet = 
    2;
            
    goto _RELEASE_APP;
        }

        
    try
        {
            pDoc = pDocs->Open(&(_variant_t(inputFileName.c_str()))); 
            
    if(pDoc == NULL)
                
    goto _RELEASE_APP;

            pDoc->ExportAsFixedFormat(szBstrOutputFileName,Word::WdExportFormat::wdExportFormatPDF,VARIANT_FALSE,
                Word::WdExportOptimizeFor::wdExportOptimizeForPrint,Word::WdExportRange::wdExportAllDocument,
    1,1,
                Word::WdExportItem::wdExportDocumentContent,VARIANT_TRUE,VARIANT_TRUE,
                Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);

            pDoc-> Close(); 
            pDoc.Release(); 
            pDoc = 
    NULL
        }
        
    catch(...)
        {
            nRet = 
    3;
        }

    _RELEASE_APP:
        pWordApp->Quit(); 
        pWordApp.Release(); 
        pWordApp = 
    NULL

        
    return nRet;
    }

    int Excel2Pdf(std::wstring inputFileName,std::wstring outputFileName)
    {
        HRESULT hr = S_OK;
        
    int nRet = 0;
        Excel::_ApplicationPtr pApplication = 
    NULL;
        Excel::_WorkbookPtr pThisWorkbook = 
    NULL ;
        BSTR szBstrInputFileName;
        BSTR szBstrOutputFileName;

        szBstrInputFileName = SysAllocString(inputFileName.c_str());    
        szBstrOutputFileName = SysAllocString(outputFileName.c_str());

        
    if (FAILED(pApplication.CreateInstance(__uuidof(Excel::Application))))
        {
            wprintf(L
    "CreateInstance failed w/err 0x%08lx ", hr);
            
    return 1;
        } 
        
    try
        {
            pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);
            pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF, szBstrOutputFileName);
            pThisWorkbook->Close();
            pThisWorkbook.Release();
            pThisWorkbook = 
    NULL;

        }
    catch(...)
        {
            nRet = 
    2;
        }

        pApplication-> Quit(); 
        pApplication.Release(); 
        pApplication= 
    NULL;

        
    return nRet;
    }

    int Ppt2Pdf(std::wstring inputFileName,std::wstring outputFileName)
    {
        PowerPoint::_ApplicationPtr spPpApp = 
    NULL
        PowerPoint::PresentationsPtr  spPres = 
    NULL;
        PowerPoint::_PresentationPtr  pPre = 
    NULL;

        BSTR szBstrInputFileName;
        BSTR szBstrOutputFileName;
        BSTR szBstrEmpty;
        HRESULT hr = S_OK;
        
    int nRet = 0;

        szBstrInputFileName = SysAllocString(inputFileName.c_str());    
        szBstrOutputFileName = SysAllocString(outputFileName.c_str());
        szBstrEmpty = SysAllocString(L
    "");



        
    if (FAILED(spPpApp.CreateInstance(__uuidof(PowerPoint::Application)))) 
        { 
            wprintf(L
    "CreateInstance failed w/err 0x%08lx ", hr); 
            
    return 1
        } 

        spPres = spPpApp->Presentations; 
        
    if(spPres==NULL)
        {
            nRet = 
    2;
            
    goto _RELEASE_APP;
        }

        
    try
        {
            pPre = spPres->Open(szBstrInputFileName,
                Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
            
    if(pPre ==NULL)
            {
                nRet = 
    3;
                
    goto _RELEASE_APP;
            }

            pPre->ExportAsFixedFormat(szBstrOutputFileName,PowerPoint::PpFixedFormatType::ppFixedFormatTypePDF,
                PowerPoint::PpFixedFormatIntent::ppFixedFormatIntentPrint,Office::MsoTriState::msoTriStateMixed,
                PowerPoint::PpPrintHandoutOrder::ppPrintHandoutHorizontalFirst,PowerPoint::PpPrintOutputType::ppPrintOutputSlides,
                Office::MsoTriState::msoFalse,
    NULL,PowerPoint::PpPrintRangeType::ppPrintAll,szBstrEmpty,
                VARIANT_TRUE,VARIANT_FALSE,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);
            pPre->Close();
            pPre.Release();
            pPre = 
    NULL;
        }
        
    catch(...)
        {
            nRet = 
    4;
        }

    _RELEASE_APP:
        spPpApp->Quit(); 
        spPpApp.Release(); 
        spPpApp = 
    NULL;

        
    return nRet;
    }
  • 相关阅读:
    char、varchar、nchar、nvarchar的区别
    linux和windows下分别如何查看电脑是32位的还是64位?
    HP-Unix安装Memcache问题
    安装GCC-4.6.1详细教程
    JSTL 核心标签库 使用
    JSP && EL表达式
    UNIX环境高级编程——标准IO-实现查看所有用户
    UNIX环境高级编程——环境变量表读取/添加/修改/删除
    UNIX网络编程——进程间通信概述
    UNIX网络编程——通过UNIX域套接字传递描述符和 sendmsg/recvmsg 函数
  • 原文地址:https://www.cnblogs.com/MakeView660/p/6929982.html
Copyright © 2011-2022 走看看