Knowledgebase (2417)
Children categories
Excel is a wonderful tool for the creation and management of spreadsheets. However, when it comes to sharing these files with others, Excel may not deliver the best results. As soon as you have finalized your report, you can convert it to PDF, which keeps the formatting of your spreadsheets and allows them to be displayed perfectly on a variety of devices. Furthermore, PDFs are secure and can be encrypted to prevent unauthorized changes to the content.
In this article, you will learn how to convert an Excel workbook to PDF and how to convert an Excel worksheet to PDF in C++ using Spire.XLS for C++.
Install Spire.XLS for C++
There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.XLS for C++ in a C++ Application
Convert an Excel Workbook to a PDF Document in C++
Spire.XLS for C++ offers the Workbook->SaveToFile(LPCWSTR_S fileName, FileFormat fileFormat) method, enabling users to convert an entire workbook to another format file, like PDF, HTML, CSV and XPS. Besides, it offers the ConverterSetting class to specify the convert options, such as whether to automatically adjust the height and width of the cells during conversion. The following are the steps to convert an Excel workbook to PDF using it.
- Create a Workbook object.
- Load a sample Excel document using Workbook->LoadFromFile() method.
- Make worksheets to fit to page when converting using Workbook->GetConverterSetting()->SetSheetFitToPage() method.
- Convert the workbook to PDF using Workbook->SaveToFile() method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
using namespace std;
int main()
{
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.xlsx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load the source Excel file
workbook->LoadFromFile(inputFilePath.c_str());
//Set worksheets to fit to page when converting
workbook->GetConverterSetting()->SetSheetFitToPage(true);
//Save to PDF
workbook->SaveToFile(outputFile.c_str(), FileFormat::PDF);
workbook->Dispose();
}

Convert a Specific Worksheet to a PDF Document in C++
To export a specific worksheet as a PDF, you must first use the Workbook->GetWorksheets()->Get(index) method to obtain the worksheet, and then use the Worksheet->SaveToPdf(LPCWSTR_S fileName) method to save it. The following are the detailed steps.
- Create a Workbook object.
- Load a sample Excel document using Workbook->LoadFromFile() method.
- Make worksheets to fit to page when converting using Workbook->GetConverterSetting()->SetSheetFitToPage() method.
- Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
- Convert the worksheet to PDF using Worksheet->SaveToPdf() method.
- C++
#include "Spire.Xls.o.h"
using namespace Spire::Xls;
using namespace std;
int main()
{
//Specify input file path
wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.xlsx";
//Specify output file path and name
wstring outputPath = L"Output\\";
wstring outputFile = outputPath + L"ToPDF.pdf";
//Create a Workbook object
intrusive_ptr<Workbook> workbook = new Workbook();
//Load the source Excel file
workbook->LoadFromFile(inputFilePath.c_str());
//Set worksheets to fit to page when converting
workbook->GetConverterSetting()->SetSheetFitToPage(true);
//Get a specific worksheet
intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));
//Save it to PDF
sheet->SaveToPdf(outputFile.c_str());
workbook->Dispose();
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails from any .NET (C#, VB.NET, ASP.NET, .Net Core, .Net Standard, .NET 5.0, MonoAndroid, Xamarin iOS) platform with fast and high quality performance.
As an independent .NET Email API, Spire.Email for .NET doesn't need Microsoft outlook to be installed on the machine. However, it can incorporate Microsoft Outlook document creation capabilities into any developers' .NET applications.
Spire.PDFViewer for .NET is a powerful PDF Viewer library for .NET. It allows developers to load PDF document from stream, file and byte array. Spire.PDFViewer is available on viewing PDF/A-1B, PDF/X1A and enables to open and read encrypted PDF files. This PDF Viewer control supports multiple printing orientation including landscape, portrait and automatic.
Furthermore, it can export PDFs to popular image formats like .bmp, .png and .jpeg. When viewing PDF document through Spire.PDFViewer, users can set display as fit page, page down/up, zoom in/out, etc. Spire.PDFViewer is a totally independent .NET library which designed for viewing PDF files from .NET application. It does NOT require Adobe Reader or any other 3rd party software/library installed on system.



