用C++合并或取消合并单元格范围
Contents
[
Hide
]
您可以使用 Aspose.Cells 来合并或拆分一系列单元格。Aspose.Cells 为此提供了 Range.Merge() 和 Range.UnMerge() 方法。本文解释了如何将一系列单元格合并为单个单元格。
示例
以下示例代码首先创建一个范围 — A1:D4 — 然后使用Range.Merge()方法将该范围中的单元格合并成一个单元格。类似地,你也可以通过创建范围并调用Range.UnMerge()方法来拆分单元格。
#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
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Path of output excel file
U16String outputPath = srcDir + u"output.out.xlsx";
// Create a workbook
Workbook workbook;
// Access the first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Create a range
Range range = worksheet.GetCells().CreateRange(u"A1:D4");
// Merge range into a single cell
range.Merge();
// Save the workbook
workbook.Save(outputPath);
std::cout << "Workbook saved successfully!" << std::endl;
Aspose::Cells::Cleanup();
}