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类型库

#import "C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\MSO.DLL" \
    rename("RGB","_OfficeRGB")

#import "C:\Program Files (x86)\Common Files\microsoft shared\VBA\VBA6\VBE6EXT.OLB" \
    rename("Reference", "ignorethis")

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

#import "C:\Program Files (x86)\Microsoft Office\Office14\MSPPT.OLB"
#import "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.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[])
{
    ;
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        std::cout << "Initialize COM failed..." << std::endl;
        ;
    }

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

)
    {
        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;
        ;
    }

wsCmd = argv[];
    wsS   = argv[];
    wsD   = argv[];

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

)
    {
        std::cout << "Error:" << nRet << std::endl;
    }

return nRet;
}

//实现
int Word2Pdf(std::wstring inputFileName,std::wstring outputFileName)  
{
    ;
    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)
    {
        ;
    }

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

if(pDocs==NULL)
    {
        nRet = ;
        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,,
            Word::WdExportItem::wdExportDocumentContent,VARIANT_TRUE,VARIANT_TRUE,
            Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);

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

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

return nRet;
}

int Excel2Pdf(std::wstring inputFileName,std::wstring outputFileName)
{
    HRESULT hr = S_OK;
    ;
    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\n", hr);
        ;
    } 
    try
    {
        pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);
        pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF, szBstrOutputFileName);
        pThisWorkbook->Close();
        pThisWorkbook.Release();
        pThisWorkbook = NULL;

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

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

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\n", hr); 
        ; 
    }

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

try
    {
        pPre = spPres->Open(szBstrInputFileName,
            Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
        if(pPre ==NULL)
        {
            nRet = ;
            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 = ;
    }

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

return nRet;
}

Microsoft Word、Excel、PowerPoint转Pdf的更多相关文章

  1. Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

    Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...

  2. word/excel/ppt 2 PDF

    PHP 实现 word/excel/ppt 转换为 PDF 一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码: class PDFConverter { private $com; ...

  3. C#.net word excel powerpoint (ppt) 转换成 pdf 文件

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. word,excel,ppt转Pdf,Pdf转Swf,通过flexpaper+swftools实现在线预览

    其实这是我好几年前的项目,现在再用这种方式我也不建议了,毕竟未来flash慢慢会淘汰,此方式也是因为目测大部分人都装了flash,才这么做的,但是页面展示效果也不好.其实还是考虑收费的控件,毕竟收费的 ...

  5. word,excel,ppt转pdf

    第一步 需要下载jar包和jacob-1.14.3-x64.dll * <dependency> * <groupId>net.sf.jacob-project</gro ...

  6. C#将Word,Excel与Html,PDF互转

    public class OfficeHelper { /// <summary> /// word转成html /// </summary> /// <param na ...

  7. DataTable导出为word,excel,html,csv,pdf,.txt

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  8. PHP 实现 word/excel/ppt 转换为 PDF

    前段时间负责公司内部文件平台的设计,其中有一个需求是要能够在线浏览用户上传的 office 文件. 我的思路是先将 office 转换成 PDF,再通过 pdf.js 插件解析 PDF 文件,使其能在 ...

  9. 21.PHP实现Word/Excel/PPT转换为PDF

    参考文档: https://www.cnblogs.com/woider/p/7003481.html http://blog.csdn.net/aoshilang2249/article/detai ...

随机推荐

  1. JDBC 关于大文本数据

    大文本数据Clob,在不同的数据库中类型名不一致,有的是text格式,有的是clob,还有其他一些格式   package test; import java.io.BufferedReader; i ...

  2. 用vue开发顶端粘滞效果的页面

    概述 通常一个长的页面,需要滚动浏览,有部分重要信息会随着滚动而看不见,因此需要粘滞在顶端,而又不影响滚动浏览,这个demo基于vue2,实现这个需求. 详细 代码下载:http://www.demo ...

  3. Linux-软件包管理-rpm命令管理-查询

    rpm -q httpd 查看apache包是否已经安装 rpm -qa 查看所有已经安装的包rpm -qa | grep httpd 查询包含和apache关键字相关联的所有包信息 rpm -qi ...

  4. 解决C# WINFORM程序只允许运行一个实例的几种方法详解

    要实现程序的互斥,通常有下面几种方式,下面用 C# 语言来实现: 方法一: 使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例. 把program.cs文件里的Main()函数改为如下代码: u ...

  5. spring in action 9.1 spring security

    spring security是基于spring AOP 和 Servlet 规范中的Filter 实现的安全框架. Spring Security 是为基于 Spring 的应用程序提供声明式安全保 ...

  6. MongoDB 学习 第八节 驱动实践

    作为系列的最后一篇,得要说说C#驱动对mongodb的操作,目前驱动有两种:官方驱动和samus驱动,不过我个人还是喜欢后者, 因为提供了丰富的linq操作,相当方便. 官方驱动:https://gi ...

  7. vue2.0实现图片加载失败默认显示图片

    <div class="bg"> <img :src="goods.phoneFloorAd.resUrl" :onerror="e ...

  8. js基本知识2

    一.提示框 1. 弹出警示框 alert(); window.alert(); window 窗口 2. 控制台输出 console.log() 3. 文档打印 document 文档 documen ...

  9. 【手把手教你全文检索】Apache Lucene初探 (zhuan)

    http://www.cnblogs.com/xing901022/p/3933675.html *************************************************** ...

  10. SAP ECC6安装系列一:安装前硬件和软件准备

    原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 写在前面的罗嗦话 一晃就是5年,前几天 ...