使用 C++ 合并与取消合并单元格

介绍

并非总是希望每行或每列中都有相同数量的单元格。例如,您可能希望在一个跨越多个列的单元格中放置标题。或者,如果创建发票,则可能希望总计列中的列数较少。将两个或多个单元格合并成一个单元格,以实现此目的。Microsoft Excel允许用户选择文件并将其合并以按照自己的方式构造电子表格。

在工作表中合并单元格

在Microsoft Excel中合并单元格

以下步骤描述如何在MS Excel中合并工作表中的单元格。

  1. 将要复制的数据复制到范围内左上角的单元格中。
  2. 选择要合并的单元格。
  3. 要合并行或列中的单元格并将单元格内容居中,点击合并和居中图标上的格式工具栏。

使用Aspose.Cells合并单元格

Aspose::Cells::Cells 类提供了一些有用的方法。例如,Merge() 方法可以将指定范围内的单元格合并成一个单元格。

以下示例显示了如何在工作表中合并单元格(C6:E7)。

#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Create a Workbook
    Workbook wbk;

    // Create a Worksheet and get the first sheet
    Worksheet worksheet = wbk.GetWorksheets().Get(0);

    // Create a Cells object to fetch all the cells
    Cells cells = worksheet.GetCells();

    // Merge some Cells (C6:E7) into a single C6 Cell
    cells.Merge(5, 2, 2, 3);

    // Input data into C6 Cell
    worksheet.GetCells().Get(5, 2).PutValue(u"This is my value");

    // Create a Style object to fetch the Style of C6 Cell
    Style style = worksheet.GetCells().Get(5, 2).GetStyle();

    // Create a Font object
    Font font = style.GetFont();

    // Set the name
    font.SetName(u"Times New Roman");

    // Set the font size
    font.SetSize(18);

    // Set the font color
    font.SetColor(Color::Blue());

    // Bold the text
    font.SetIsBold(true);

    // Make it italic
    font.SetIsItalic(true);

    // Set the background color of C6 Cell to Red
    style.SetForegroundColor(Color::Red());
    style.SetPattern(BackgroundType::Solid);

    // Apply the Style to C6 Cell
    worksheet.GetCells().Get(5, 2).SetStyle(style);

    // Save the Workbook
    wbk.Save(outDir + u"mergingcells.out.xls");

    std::cout << "Workbook saved successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

取消合并(拆分)合并的单元格

使用Microsoft Excel

以下是使用Microsoft Excel拆分合并单元格的步骤。

  1. 选择已合并的单元格。 当单元格已合并时,在格式工具栏上选择合并和居中
  2. 格式工具栏上点击合并和居中

使用Aspose.Cells

Aspose::Cells::Cells 类具有 UnMerge() 方法,可以将单元格拆分回原始状态。该方法通过合并单元格的引用进行取消合并。

以下示例显示了如何拆分合并的单元格(C6)。该示例使用上一个示例中创建的文件,并拆分了合并的单元格。

#include <iostream>
#include "Aspose.Cells.h"

using namespace Aspose::Cells;

int main()
{
    Aspose::Cells::Startup();

    // Source directory path
    U16String srcDir(u"..\\Data\\01_SourceDirectory\\");

    // Output directory path
    U16String outDir(u"..\\Data\\02_OutputDirectory\\");

    // Path of input excel file
    U16String inputFilePath = srcDir + u"mergingcells.xls";

    // Path of output excel file
    U16String outputFilePath = outDir + u"unmergingcells.out.xls";

    // Create a Workbook and open the excel file
    Workbook wbk(inputFilePath);

    // Get the first worksheet
    Worksheet worksheet = wbk.GetWorksheets().Get(0);

    // Get the Cells object to fetch all the cells
    Cells cells = worksheet.GetCells();

    // Unmerge the cells
    cells.UnMerge(5, 2, 2, 3);

    // Save the file
    wbk.Save(outputFilePath);

    std::cout << "Cells unmerged successfully!" << std::endl;

    Aspose::Cells::Cleanup();
}

高级主题