检测工作表中的合并单元
Contents
[
Hide
]
本文介绍如何在工作表中获取合并的单元区域。
Aspose.Cells允许您在工作表中获取合并的单元区域。您也可以取消合并(拆分)它们。本文展示了使用Aspose.Cells API执行任务的最简单的代码。
该组件提供了能够获取所有合并单元格的方法。以下代码示例向您展示了如何在工作表中检测合并单元格。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook | |
// Open an existing excel file | |
Workbook wkBook = new Workbook(dataDir + "SampleInput.xlsx"); | |
// Get a worksheet in the workbook | |
Worksheet wkSheet = wkBook.Worksheets["Sheet2"]; | |
// Clear its contents | |
wkSheet.Cells.Clear(); | |
// Get merged areas | |
CellArea[] areas = wkSheet.Cells.GetMergedAreas(); | |
// Define some variables | |
int frow, fcol, erow, ecol, trows, tcols; | |
// Loop through the arraylist and get each cellarea | |
// To unmerge it | |
for (int i = 0; i < areas.Length; i++) | |
{ | |
frow = areas[i].StartRow; | |
fcol = areas[i].StartColumn; | |
erow = areas[i].EndRow; | |
ecol = areas[i].EndColumn; | |
trows = erow - frow + 1; | |
tcols = ecol - fcol + 1; | |
wkSheet.Cells.UnMerge(frow, fcol, trows, tcols); | |
} | |
dataDir = dataDir + "MergeTrial.out.xlsx"; | |
// Save the excel file | |
wkBook.Save(dataDir); |