在 Python 中對 Excel 列/欄進行分組與取消分組
from ironxl import *

# Load existing spreadsheet
workbook = WorkBook.Load("sample.xlsx")
worksheet = workbook.DefaultWorkSheet

# Apply grouping to row 1-5
worksheet.GroupRows(0, 4)

# Ungroup row 3-5
worksheet.UngroupRows(2, 4)

# Apply grouping to column A-F
worksheet.GroupColumns(0, 5)

# Ungroup column C-D will cut the grouping at B
worksheet.UngroupColumn("C", "D")

# Save changes with grouped and ungrouped rows and columns
workbook.SaveAs("groupAndUngroup.xlsx")
pip install ironxl
在 Python 中對 Excel 列/欄進行分組與取消分組

在 Python 中對 Excel 列/欄進行分組與取消分組

在Excel中使用直觀的IronXL for Python API來應用行和列的分組和取消分組,而無需Office Interop。

分組和取消分組行

GroupRows方法通過指定行的索引位置來分組。 可以通過多次調用此方法來應用多個分組。

要取消分組行,請使用UngroupRows方法。 此方法就像一個切割工具——當應用於現有行組的中間時,它會將該組分成兩個。 例如,在0-9跨度的組中取消分組2-4行,會產生0-1和5-9的組。

分組和取消分組列

列的分組方式與行相同。 使用GroupColumns方法可以通過索引位置或將列名稱指定為字串來分組列。 可以根據需要創建多個列組。

要取消分組列,請使用UngroupColumns方法,此方法同時也具備剪切工具的功能。 將其應用於列組的中間會將其分成兩部分。 例如,將群組 AF 中的列 CD 取消分組,會得到群組 AB 和 EF。

所有索引位置均使用從零開始的索引。 分組功能只能套用於包含值的儲存格。

學習如何使用 IronXL for Python 分組和取消分組 Excel 的行和列

準備好開始了嗎?

版本:2026.11剛剛發布