检测工作表中的合并单元格
Contents
[
Hide
]
本文介绍如何在工作表中获取合并的单元区域。
Aspose.Cells允许您在工作表中获取合并的单元区域。您也可以取消合并(拆分)它们。本文展示了使用Aspose.Cells API执行任务的最简单的代码。
该组件提供 Cells::GetMergedAreas() 方法,可以获取所有合并单元格。以下示例代码演示如何在工作表中检测合并单元格。
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
Workbook workbook(srcDir + u"SampleInput.xlsx");
Worksheet wkSheet = workbook.GetWorksheets().Get(u"Sheet2");
wkSheet.GetCells().Clear();
Vector<CellArea> areas = wkSheet.GetCells().GetMergedAreas();
for (int i = 0; i < areas.GetLength(); ++i)
{
int frow = areas[i].StartRow;
int fcol = areas[i].StartColumn;
int erow = areas[i].EndRow;
int ecol = areas[i].EndColumn;
int trows = erow - frow + 1;
int tcols = ecol - fcol + 1;
wkSheet.GetCells().UnMerge(frow, fcol, trows, tcols);
}
U16String outputPath = outDir + u"MergeTrial.out.xlsx";
workbook.Save(outputPath);
std::cout << "Worksheet processing completed successfully." << std::endl;
Aspose::Cells::Cleanup();
}