C++: Apply Fonts to Excel Cells

While creating a worksheet, the default font MS Excel uses is "Calibri" with a size of 11 and a color of black. However, there are times when you may want to apply a different font style such as bold, italic to optimize the appearance of your document, or set a unique font color to highlight useful information within a large set of data. In this article, you will learn how to programmatically apply multiple font styles to Excel cells 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

Apply Different Fonts to Different Excel Cells in C++

The ExcelFont class provided by Spire.XLS for C++ allows you to set or change the font name, color, size, and style in a cell easily. The following are the steps to apply different fonts to different cells in Excel.

  • Create a Workbook object.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Get a specified cell using Worksheet->GetRange() method.
  • Set the value of the cell using CellRange->SetText() method.
  • Get the font in the specified cell using CellRange->GetStyle()->GetFont() method.
  • Set the font name, color, size and style using the methods under the ExcelFont class.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

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

	//Create a Workbook object
	Workbook* workbook = new Workbook();

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

	//Set font name
	sheet->GetRange(L"B1")->SetText(L"Font name: Comic Sans MS");
	sheet->GetRange(L"B1")->GetStyle()->GetFont()->SetFontName(L"Comic Sans MS");

	//Set font size
	sheet->GetRange(L"B2")->SetText(L"Font size: 25");
	sheet->GetRange(L"B2")->GetStyle()->GetFont()->SetSize(25);

	//Set text to bold
	sheet->GetRange(L"B3")->SetText(L"Font style: Bold");
	sheet->GetRange(L"B3")->GetStyle()->GetFont()->SetIsBold(true);

	//Underline text
	sheet->GetRange(L"B4")->SetText(L"Font style: Underline");
	sheet->GetRange(L"B4")->GetStyle()->GetFont()->SetUnderline(FontUnderlineType::Single);

	//Set font color
	sheet->GetRange(L"B5")->SetText(L"Font color: Red");
	sheet->GetRange(L"B5")->GetStyle()->GetFont()->SetColor(Spire::Common::Color::GetRed());

	//Set text to italic
	sheet->GetRange(L"B6")->SetText(L"Font style: Italic");
	sheet->GetRange(L"B6")->GetStyle()->GetFont()->SetIsItalic(true);

	//Add strikethrough to text
	sheet->GetRange(L"B7")->SetText(L"Font style: Strikethrough");
	sheet->GetRange(L"B7")->GetStyle()->GetFont()->SetIsStrikethrough(true);

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

C++: Apply Fonts to Excel Cells

Apply Multiple Fonts to a Single Excel Cell in C++

Mixing different fonts in one cell can help you emphasize some specific characters. The following are the steps to apply multiple fonts in an Excel cell.

  • Create a Workbook object.
  • Get a specific worksheet using Workbook->GetWorksheets()->Get() method.
  • Create two ExcelFont objects using Workbook->CreateExcelFont() method.
  • Get a specified cell using Worksheet->GetRange() method, and then set the rich text content of the cell using CellRange->GetRichText()->SetText() method.
  • Apply the two ExcelFont objects to the rich text using RichText->SetFont(int startPos, int endPos, ExcelFont* font) method.
  • Save the result file using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

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

	//Create a Workbook object
	Workbook* workbook = new Workbook();

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

	//Create a ExcelFont object and set its font style, color and size
	ExcelFont* font1 = workbook->CreateExcelFont();
	font1->SetKnownColor(ExcelColors::LightBlue);
	font1->SetIsBold(true);
	font1->SetSize(13);

	//Create another ExcelFont object and set its font style, color and size
	ExcelFont* font2 = workbook->CreateExcelFont();
	font2->SetKnownColor(ExcelColors::Red);
	font2->SetIsBold(true);
	font2->SetIsItalic(true);
	font2->SetFontName(L"Times New Roman");
	font2->SetSize(15);

	//Write a RichText string to cell B5
	RichText* richText = sheet->GetRange(L"B5")->GetRichText();
	richText->SetText(L"This document was created with Spire.XLS for C++.");

	//Apply two fonts to the text in the cell B5
	richText->SetFont(0, 29, font1);
	richText->SetFont(31, 48, font2);

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

C++: Apply Fonts to Excel Cells

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.