3分钟学会在C ++中以编程方式合并Excel工作表中的单元格

718 阅读5分钟

合并和取消合并单元格是Microsoft Excel的一项简单且常用功能。合并单元格可能会在某些情况下很有用,例如,当工作表中有多个列共享相同的标题时,可以合并列上方的单元格以使其具有共同的标题。如果不再需要合并的单元格,则可以轻松地取消合并它们。为此,本文将教您如何使用C ++以编程方式合并和取消合并Excel工作表中的单元格。

  • 使用C ++在Excel工作表中合并单元格
  • 使用C ++取消Excel工作表中的单元格
  • 使用C ++合并Excel工作表中的一系列单元格
  • 使用C ++取消Excel工作表中的单元格范围
  • 使用C ++合并Excel工作表中命名范围的单元格

Aspose.Cells for C++是本机C ++库,使用它可以创建,读取和修改Excel文件,而无需安装Microsoft Excel。该API还支持合并和取消合并Excel工作表中的单元格。下载Aspose.Cells for C++

使用C ++在Excel工作表中合并单元格

在此示例中,我们将创建一个空的Excel工作表,并按照以下步骤合并几个单元格。

  • 首先,创建一个IWorkbook类的实例。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法检索工作表。
  • 使用IWorksheet->GetICells()方法来检索单元格。
  • 使用ICells->Merge(Aspose::Cells::Systems::Int32 firstRow, Aspose::Cells::System::Int32 firstColumn, Aspose::Cells::System::Int32 totalRows, Aspose::Cells::System::Int32 totalColumns) 方法合并这些单元格。
  • 对合并后的单元格应用样式。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存输出的Excel文件。

下面的示例代码显示了如何使用C ++合并Excel工作表中的单元格。

// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");

// Load the input Excel file
intrusive_ptrworkbook = Factory::CreateIWorkbook();

// Access the first worksheet in the Excel file
intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);

// Create a Cells object ot fetch all the cells.
intrusive_ptrcells = 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_ptrstyle = cells->GetICell(5, 2)->GetIStyle();

// Create a Font object
intrusive_ptrfont = 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")));

无需MS Office创建Excel!使用C ++以编程方式合并Excel工作表中的单元格

使用C ++取消Excel工作表中的单元格

以下是取消合并Excel工作表中单元格的步骤。

  • 使用 IWorkbook 类加载 Excel 文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法检索包含合并单元格的工作表。
  • 使用IWorksheet->GetICells()方法来检索单元格。
  • 使用ICells->UnMerge(Aspose::Cells::Systems::Int32 firstRow, Aspose::Cells::Systems::Int32 firstColumn, Aspose::Cells::Systems::Int32 totalRows, Aspose::Cells::Systems::Int32 totalColumns) 方法取消合并单元。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存输出的Excel文件。

下面的示例代码演示了如何使用C ++取消合并Excel工作表中的单元格。

// 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")));

无需MS Office创建Excel!使用C ++以编程方式合并Excel工作表中的单元格

使用C ++合并Excel工作表中的一系列单元格

以下是合并Excel工作表中的一系列单元格的步骤。

  • 使用 IWorkbook 类加载 Excel 文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法来检索所需的工作表。
  • 在单元格中插入数据。
  • 使用IWorksheet->GetICells()->CreateIRange(intrusive_ptraddress)方法创建范围。
  • 使用IRange->Merge()方法合并该范围。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存输出的Excel文件。

下面的示例代码显示了如何使用C ++合并Excel工作表中的一系列单元格。

// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");

// Load the input Excel file
intrusive_ptrworkbook = Factory::CreateIWorkbook();

// Access the first worksheet in the Excel file
intrusive_ptrworksheet = 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_ptrrange = 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")));

无需MS Office创建Excel!使用C ++以编程方式合并Excel工作表中的单元格

用C++合并Excel工作表中的单元格区域

以下是在Excel工作表中取消合并单元格区域的步骤。

  • 使用ICworkbook类加载Excel文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法检索包含合并单元格的工作表。
  • 使用IWorksheet->GetICells()->CreateIRange(intrusive_ptraddress)方法创建范围。
  • 使用IRange->UnMerge()方法取消合并该范围。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存输出的 Excel 文件。

下面的示例代码显示了如何使用C++在Excel工作表中取消合并一个单元格的范围。

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");

// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");

// Load the input Excel file
intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx")));

// Access the first worksheet in the Excel file
intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);

// Create a range
intrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4"));

// UnMerge range
range->UnMerge();

// Save the Excel file
workbook->Save(outDir->StringAppend(new String("UnmergeRangeOfCells_out.xlsx")));

无需MS Office创建Excel!使用C ++以编程方式合并Excel工作表中的单元格

使用C++合并Excel工作表中的指定范围的单元格

Aspose.Cells for C++还提供了合并指定区域的单元格的能力。为了实现这一功能,请按照下面的步骤进行操作。

  • 使用ICworkbook类加载Excel文件。
  • 使用 IWorkbook->GetIWorksheets()->GetObjectByIndex (Aspose::Cells::Systems::Int32 index) 方法检索工作表。
  • 使用IWorksheet->GetICells()->CreateIRange(intrusive_ptraddress)方法创建范围。
  • 使用IRange->SetName(intrusive_ptrvalue)方法设置范围的名称。
  • 为命名的单元格创建并应用样式。
  • 使用IRange->Merge()方法合并命名区域的单元格。
  • 最后,使用 IWorkbook->Save (intrusive_ptrfileName) 方法保存输出的Excel文件。

下面的示例代码演示了如何使用C++合并一个命名区域的单元格。

// Source directory path.
StringPtr srcDir = new String("SourceDirectory\\");

// Output directory path.
StringPtr outDir = new String("OutputDirectory\\");

// Load the input Excel file
intrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx")));

// Access the first worksheet in the Excel file
intrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);

// Create a range
intrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4"));

// Set Range name
range->SetName(new String("Named_Range"));

// Define style object
intrusive_ptrstyle = workbook->CreateIStyle();

// Set horizontal alignment
style->SetHorizontalAlignment(TextAlignmentType::TextAlignmentType_Center);

// Create a StyleFlag object
intrusive_ptrstyleFlag = 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")));

无需MS Office创建Excel!使用C ++以编程方式合并Excel工作表中的单元格

如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。