Last active
May 25, 2021 11:14
Merge or Unmerge Cells in Excel Worksheet using C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Merge or Unmerge Cells in Excel Worksheet using C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Output directory path. | |
StringPtr outDir = new String("OutputDirectory\\"); | |
// Load the input Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Access the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create a Cells object ot fetch all the cells. | |
intrusive_ptr<ICells> cells = worksheet->GetICells(); | |
// Merge cells | |
cells->Merge(5, 2, 2, 3); | |
// Put data into the cell | |
cells->GetObjectByIndex(5, 2)->PutValue((StringPtr)new String("This is a test value")); | |
// Create a Style object | |
intrusive_ptr<IStyle> style = cells->GetICell(5, 2)->GetIStyle(); | |
// Create a Font object | |
intrusive_ptr<Aspose::Cells::IFont> font = style->GetIFont(); | |
// Set the name | |
font->SetName(new String("Times New Roman")); | |
// Set the font size | |
font->SetSize(18); | |
// Set the font color | |
font->SetColor(Systems::Drawing::Color::GetCyan()); | |
// Make the text bold | |
font->SetBold(true); | |
// Make the text italic | |
font->SetItalic(true); | |
// Set the Foreground color | |
style->SetForegroundColor(Systems::Drawing::Color::GetRed()); | |
// Set the Pattern | |
style->SetPattern(BackgroundType_Solid); | |
// Apply the Style | |
cells->GetICell(5, 2)->SetIStyle(style); | |
// Save the Excel file | |
workbook->Save(outDir->StringAppend(new String("MergeCells_out.xlsx"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source directory path. | |
StringPtr srcDir = new String("SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("OutputDirectory\\"); | |
// Load the input Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx"))); | |
// Access the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create a range | |
intrusive_ptr<IRange> range = worksheet->GetICells()->CreateIRange(new String("A1:D4")); | |
// Set Range name | |
range->SetName(new String("Named_Range")); | |
// Define style object | |
intrusive_ptr<IStyle> style = workbook->CreateIStyle(); | |
// Set horizontal alignment | |
style->SetHorizontalAlignment(TextAlignmentType::TextAlignmentType_Center); | |
// Create a StyleFlag object | |
intrusive_ptr<IStyleFlag> styleFlag = Factory::CreateIStyleFlag(); | |
// Set horizontal alignment to true | |
styleFlag->SetHorizontalAlignment(true); | |
// Apply the style to the range | |
range->ApplyIStyle(style, styleFlag); | |
// Put data in the range | |
range->GetObjectByIndex(0, 0)->PutValue((StringPtr)new String("This is a test value")); | |
// Merge Range | |
range->Merge(); | |
// Save the Excel file | |
workbook->Save(outDir->StringAppend(new String("MergeNamedRange_out.xlsx"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Output directory path. | |
StringPtr outDir = new String("OutputDirectory\\"); | |
// Load the input Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(); | |
// Access the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Input data into A1 Cell. | |
worksheet->GetICells()->GetObjectByIndex(0, 0)->PutValue((StringPtr)new String("This is a test value")); | |
// Create a range | |
intrusive_ptr<IRange> range = worksheet->GetICells()->CreateIRange(new String("A1:D4")); | |
// Merge range into a single cell | |
range->Merge(); | |
// Save the Excel file | |
workbook->Save(outDir->StringAppend(new String("MergeRangeOfCells_out.xlsx"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source directory path. | |
StringPtr srcDir = new String("SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("OutputDirectory\\"); | |
// Load the input Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedCells.xlsx"))); | |
// Access the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create a Cells object ot fetch all the cells. | |
intrusive_ptr<ICells> cells = worksheet->GetICells(); | |
// Unmerge cells. | |
cells->UnMerge(5, 2, 2, 3); | |
// Save the Excel file | |
workbook->Save(outDir->StringAppend(new String("UnmergeCells_out.xlsx"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source directory path. | |
StringPtr srcDir = new String("SourceDirectory\\"); | |
// Output directory path. | |
StringPtr outDir = new String("OutputDirectory\\"); | |
// Load the input Excel file | |
intrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx"))); | |
// Access the first worksheet in the Excel file | |
intrusive_ptr<IWorksheet> worksheet = workbook->GetIWorksheets()->GetObjectByIndex(0); | |
// Create a range | |
intrusive_ptr<IRange> range = worksheet->GetICells()->CreateIRange(new String("A1:D4")); | |
// UnMerge range | |
range->UnMerge(); | |
// Save the Excel file | |
workbook->Save(outDir->StringAppend(new String("UnmergeRangeOfCells_out.xlsx"))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment