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

合并和取消合并单元格是Microsoft Excel的一项简单且常用功能,有时可能需要在C ++应用程序中执行这些任务。为此,本文将教您如何使用C ++以编程方式合并和取消合并Excel工作表中的单元格。

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

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


使用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 fileintrusive_ptrworkbook = Factory::CreateIWorkbook();// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Create a Cells object ot fetch all the cells.intrusive_ptrcells = worksheet->GetICells();// Merge cellscells->Merge(5, 2, 2, 3);// Put data into the cellcells->GetObjectByIndex(5, 2)->PutValue((StringPtr)new String("This is a test value"));// Create a Style objectintrusive_ptrstyle = cells->GetICell(5, 2)->GetIStyle();// Create a Font objectintrusive_ptrfont = style->GetIFont();// Set the namefont->SetName(new String("Times New Roman"));// Set the font sizefont->SetSize(18);// Set the font colorfont->SetColor(Systems::Drawing::Color::GetCyan());// Make the text boldfont->SetBold(true);// Make the text italicfont->SetItalic(true);// Set the Foreground colorstyle->SetForegroundColor(Systems::Drawing::Color::GetRed());// Set the Patternstyle->SetPattern(BackgroundType_Solid);// Apply the Stylecells->GetICell(5, 2)->SetIStyle(style);// Save the Excel fileworkbook->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 fileintrusive_ptr<IWorkbook> workbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedCells.xlsx")));// Access the first worksheet in the Excel fileintrusive_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 fileworkbook->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 fileintrusive_ptrworkbook = Factory::CreateIWorkbook();// Access the first worksheet in the Excel fileintrusive_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 rangeintrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4"));// Merge range into a single cellrange->Merge();// Save the Excel fileworkbook->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 fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Create a rangeintrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4"));// UnMerge rangerange->UnMerge();// Save the Excel fileworkbook->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 fileintrusive_ptrworkbook = Factory::CreateIWorkbook(srcDir->StringAppend(new String("SampleMergedRangeOfCells.xlsx")));// Access the first worksheet in the Excel fileintrusive_ptrworksheet = workbook->GetIWorksheets()->GetObjectByIndex(0);// Create a rangeintrusive_ptrrange = worksheet->GetICells()->CreateIRange(new String("A1:D4"));// Set Range namerange->SetName(new String("Named_Range"));// Define style objectintrusive_ptrstyle = workbook->CreateIStyle();// Set horizontal alignmentstyle->SetHorizontalAlignment(TextAlignmentType::TextAlignmentType_Center);// Create a StyleFlag objectintrusive_ptrstyleFlag = Factory::CreateIStyleFlag();// Set horizontal alignment to truestyleFlag->SetHorizontalAlignment(true);// Apply the style to the rangerange->ApplyIStyle(style, styleFlag);// Put data in the rangerange->GetObjectByIndex(0, 0)->PutValue((StringPtr)new String("This is a test value"));// Merge Rangerange->Merge();// Save the Excel fileworkbook->Save(outDir->StringAppend(new String("MergeNamedRange_out.xlsx")));

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

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【Aspose最新资源在线文库】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年4月23日
下一篇 2021年4月23日

相关推荐

发表回复

登录后才能评论