Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Sometimes, you want to change the alignment of multiple cells but also keep the existing formatting. Aspose.Cells allows you to do this using the GetAlignments() property. If you set it true, the changes in alignment will take place; otherwise, they will not. Note that the StyleFlag object is passed as a parameter to the ApplyStyle(const Style& style, const StyleFlag& flag) method, which actually applies the formatting to a range of cells.
The following sample code loads the sample Excel file, creates the range, and center‑aligns it horizontally and vertically while keeping the existing formatting intact. The screenshot below compares the sample Excel file with the output Excel file and shows that all existing formatting of the cells is the same except that the cells are now center‑aligned horizontally and vertically.

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Load sample Excel file containing cells with formatting.
U16String sourceDir(u"..\\Data\\01_SourceDirectory\\");
U16String outputDir(u"..\\Data\\02_OutputDirectory\\");
Workbook wb(sourceDir + u"sampleChangeCellsAlignmentAndKeepExistingFormatting.xlsx");
// Access first worksheet.
Worksheet ws = wb.GetWorksheets().Get(0);
// Create cells range.
Range rng = ws.GetCells().CreateRange(u"B2:D7");
// Create style object.
Style st = wb.CreateStyle();
// Set the horizontal and vertical alignment to center.
st.SetHorizontalAlignment(TextAlignmentType::Center);
st.SetVerticalAlignment(TextAlignmentType::Center);
// Create style flag object.
StyleFlag flag;
// Set the style flag's Alignments property to true. This is the most crucial statement,
// because if it is false, no changes will take place.
flag.SetAlignments(true);
// Apply style to range of cells.
rng.ApplyStyle(st, flag);
// Save the workbook in XLSX format.
wb.Save(outputDir + u"outputChangeCellsAlignmentAndKeepExistingFormatting.xlsx", SaveFormat::Xlsx);
Aspose::Cells::Cleanup();
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.