检测工作表中的合并单元
Contents
[
Hide
]
本文介绍如何在工作表中获取合并的单元区域。
Aspose.Cells for Python via .NET 允许你获取工作表中的合并单元格区域,你也可以取消合并(拆分)它们。本文展示了使用Aspose.Cells API执行此任务的最简代码。
该组件提供Cells.get_merged_areas()方法,可以获取所有合并的单元格。以下示例代码演示了如何检测工作表中的合并单元格。
This file contains hidden or 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
from aspose import pycore | |
from aspose.cells import CellArea, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a new Workbook | |
# Open an existing excel file | |
wkBook = Workbook(dataDir + "SampleInput.xlsx") | |
# Get a worksheet in the workbook | |
wkSheet = wkBook.worksheets.get("Sheet2") | |
# Clear its contents | |
wkSheet.cells.clear() | |
# Create an arraylist object | |
al = [] | |
# Get the merged cells list to put it into the arraylist object | |
al = wkSheet.cells.merged_cells | |
# Loop through the arraylist and get each cellarea | |
# To unmerge it | |
for i in range(len(al)): | |
ca = CellArea() | |
ca = pycore.cast(CellArea, al[i]) | |
frow = ca.start_row | |
fcol = ca.start_column | |
erow = ca.end_row | |
ecol = ca.end_column | |
trows = erow - frow + 1 | |
tcols = ecol - fcol + 1 | |
wkSheet.cells.un_merge(frow, fcol, trows, tcols) | |
dataDir = dataDir + "MergeTrial.out.xlsx" | |
# Save the excel file | |
wkBook.save(dataDir) |