Spire.XLS for C++ Program Guide Content

2023-02-13 07:04:19 Written by Koohji

Spire.XLS for C++ is a professional Excel C++ API that can be used to create, read, write and convert Excel files in any type of C++ application. Spire.XLS for C++ offers object model Excel API for speeding up Excel programming in C++ platform - create new Excel documents from template, edit existing Excel documents and convert Excel files.

This Professional Python Excel API is a standalone Excel Python managed assembly and does not depend on Microsoft Office Excel. It features fast and reliably compared with developing your own spreadsheet manipulation solution or using Microsoft Automation.

When viewing data in a large excel worksheet, you may often lose track of the header rows or columns when scrolling to another part of the worksheet. Under the circumstances, MS Excel provides the "Freeze Panes" function to help you lock the necessary rows or/and columns to keep them visible all the time. In this article, you will learn how to programmatically freeze rows or/and columns in an Excel worksheet using Spire.XLS for C++.

Spire.XLS for C++ provides the Worksheet->FreezePanes(int rowIndex, int columnIndex) method to freeze all rows and columns above and left of the selected cell which is specified by the rowIndex and the columnIndex.

C++: Freeze Rows and Columns in Excel

This tutorial provides the code examples for the following cases:

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

Freeze the Top Row in Excel in C++

To freeze the top row, the selected cell should be the cell (2, 1) – "A2". The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Freeze the top row using Worksheet->FreezePanes(2, 1) method.
  • Save the workbook to another Excel file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h"

using namespace Spire::Xls;
using namespace std;

int main()
{
	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"sample.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx";

	//Create a Workbook object
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load the Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Freeze top row
	sheet->FreezePanes(2, 1);

	//Save to file
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Freeze Rows and Columns in Excel

Freeze the First Column in Excel in C++

To freeze the first column, the selected cell should be the cell (1, 2) – "B1". The following are the steps to freeze the first column in an Excel worksheet.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Freeze the first column using Worksheet->FreezePanes(1, 2) method.
  • Save the workbook to another Excel file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h"

using namespace Spire::Xls;
using namespace std;

int main()
{
	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"sample.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx";

	//Create a workbook
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load the Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Freeze first column
	sheet->FreezePanes(1, 2);

	//Save to file
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Freeze Rows and Columns in Excel

Freeze the First Row and First Column in Excel in C++

If you want to freeze the top row and the first column at the same time, the selected cell should be the cell (2, 2) – "B2". The following are the detailed steps.

  • Create a Workbook object.
  • Load an Excel document using Workbook->LoadFromFile() method.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Freeze the top row and first column using Worksheet->FreezePanes(1, 2) method.
  • Save the workbook to another Excel file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h" 

using namespace Spire::Xls;
using namespace std;

int main()
{
	//Specify input file path and name
	std::wstring data_path = L"Data\\";
	std::wstring inputFile = data_path + L"sample.xlsx";

	//Specify output file path and name
	std::wstring outputPath = L"Output\\";
	std::wstring outputFile = outputPath + L"FreezeFirstRowAndColumn.xlsx";

	//Create a workbook
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load the Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Freeze top row and first column
	sheet->FreezePanes(2, 2);

	//Save to file
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Freeze Rows and Columns in Excel

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.

Merging multiple PDF files into a single PDF can help you reduce clutter and let you read, print, and share the files more easily. After merging, you only need to deal with one file instead of multiple files. In this article, you will learn how to merge multiple PDF files into a single PDF in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF 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.PDF for C++ in a C++ Application

Merge Multiple PDF Files into a Single PDF in C++

Spire.PDF for C++ offers a static method - PdfDocument::MergeFiles(std::vector<LPCWSTR_S> inputFiles) which enables you to merge multiple PDF files into a single PDF file easily. The following are the detailed steps:

  • Put the input PDF files' paths into a vector.
  • Merge the PDF files specified by the paths in the vector using PdfDocument::MergeFiles(std::vector<LPCWSTR_S> inputFiles) method.
  • Specify the output file path.
  • Save the result PDF file using PdfDocumentBase->Save() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;

int main() {
	//Put the input PDF files' paths into a vector
	std::vector<LPCWSTR_S> files = { L"Input\\File_1.pdf", L"Input\\File_2.pdf", L"Input\\File_3.pdf" };

	//Merge the PDF files specified by the paths in the vector
    boost::intrusive_ptr <PdfDocumentBase>  doc = PdfDocument::MergeFiles(files);"

	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result PDF file
	doc->Save(outputFile.c_str(), FileFormat::PDF);
	doc->Close();
}

C++: Merge Multiple PDF Files into a Single PDF

Merge Multiple PDF Files from Streams in C++

You can use the PdfDocument::MergeFiles(std::vector< Stream*> streams) method to merge multiple PDF streams into a single PDF. The following are the detailed steps:

  • Read the input PDF files into streams.
  • Put the streams into a vector.
  • Merge the PDF streams using PdfDocument::MergeFiles(std::vector< Stream*> streams) method.
  • Save the result PDF file using PdfDocumentBase->Save() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;

int main() {
	//Read the input PDF files into streams
	Stream* stream1 = new Stream(L"Input\\File_1.pdf");
	Stream* stream2 = new Stream(L"Input\\File_2.pdf");
	Stream* stream3 = new Stream(L"Input\\File_3.pdf");

    //Put the streams into a vector
	std::vector<boost::intrusive_ptr<Stream>> streams = { stream1, stream2, stream3 };
	//Merge the PDF streams
	boost::intrusive_ptr<PdfDocumentBase> doc = PdfDocument::MergeFiles(streams);


	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result PDF file
	doc->Save(outputFile.c_str(), FileFormat::PDF);
	doc->Close();
}

Merge Selected Pages of PDF Files into a Single PDF in C++

You can merge a specific page or a range of pages of multiple PDF files into a single PDF file using PdfDocument->InsertPage(PdfDocument ldDoc, int pageIndex) or PdfDocument->InsertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method. The following are the detailed steps:

  • Put the input PDF files' paths into a vector.
  • Create a vector of PdfDocument objects.
  • Iterate through the paths in the vector.
  • Load the PDF files specified by the paths using PdfDocument->LoadFromFile() method.
  • Initialize an instance of PdfDocument class to create a new PDF document.
  • Insert a specific page or a range of pages from the loaded PDF files into the new PDF using PdfDocument->InsertPage(PdfDocument ldDoc, int pageIndex) or PdfDocument->InsertPageRange(PdfDocument ldDoc, int startIndex, int endIndex) method.
  • Save the result PDF using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h"

using namespace Spire::Pdf;
using namespace std;
using namespace Spire::Common;

int main() {

	//Put the input PDF files' paths into a vector
	std::vector<std::wstring> files = { L"Input\\File_2.pdf", L"Input\\File_4.pdf" };

	//Create a vector of PdfDocument objects
	std::vector<PdfDocument*> docs(files.size());

	//Iterate through the paths in the vector
	for (int i = 0; i < files.size(); i++)
	{
		//Load the PDF files specified by the paths
		docs[i] = new PdfDocument();
		docs[i]->LoadFromFile(files[i].c_str());
	}

	//Create a new PDF document
	PdfDocument* newDoc = new PdfDocument();

	//Insert pages 1-2 of the first PDF into the new PDF
	newDoc->InsertPageRange(docs[0], 0, 1);
	//Insert page 1 of the second PDF into the new PDF
	newDoc->InsertPage(docs[1], 0);

	//Specify the output file path
	wstring outputFile = L"Output\\MergePdfs.pdf";

	//Save the result pdf file
	newDoc->SaveToFile(outputFile.c_str());

	//Close the PdfDocument objects
	newDoc->Close();
	for (auto doc : docs)
	{
		doc->Close();
	}
}

C++: Merge Multiple PDF Files into a Single PDF

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.

page 98