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 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 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 Worksheet* sheet = 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.