在 Python 中向 Excel 插入行和列
from ironxl import *

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

# Add a row before row 1
worksheet.InsertRow(0)

# Insert multiple rows before row 4
worksheet.InsertRows(3, 3)

# Add a column before column A
worksheet.InsertColumn(0)

# Insert multiple columns before column F
worksheet.InsertColumns(5, 2)

# Save changes with added rows and columns
workbook.SaveAs("addRowAndColumn.xlsx")
pip install ironxl
在 Python 中向 Excel 插入行和列

在 Python 中向 Excel 插入行和列

IronXL for Python提供了以编程方式插入单个或多个行和列的能力,无需Office Interop。

插入行

InsertRows方法将新行添加到工作表中。 行插入在指定的索引位置之前。

插入列

使用InsertColumns方法在指定索引位置前添加一个或多个列。

所有索引位置均使用从零开始的索引。

学习如何在Excel中使用Python添加行和列

准备开始了吗?

版本:2026.11刚刚发布