在 Excel 工作表中插入或删除行
Contents
[
Hide
]
创建新工作表或使用现有工作表时,您可能需要添加额外的行或列以容纳数据。在其他时候,您可能需要从工作表中的指定位置删除行或列。
Aspose.Cells 提供了两种插入和删除行的方法:Cells.InsertRows和Cells.DeleteRows.这些方法针对性能进行了优化,可以非常快速地完成工作。
要插入或删除多行,我们建议您始终使用Cells.InsertRows和Cells.DeleteRows方法而不是使用Cells.InsertRow要么删除行循环中的方法。
Aspose.Cells 与 Microsoft Excel 的工作方式相同。添加行或列时,工作表内容会向下和向右移动。删除行或列时,工作表内容会向上或向左移动。添加或删除行时,其他工作表和单元格中的任何引用都会更新。
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 Workbook object. | |
// Load a template file. | |
Workbook workbook = new Workbook(dataDir+ "book1.xlsx"); | |
// Get the first worksheet in the book. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Insert 10 rows at row index 2 (insertion starts at 3rd row) | |
sheet.Cells.InsertRows(2, 10); | |
// Delete 5 rows now. (8th row - 12th row) | |
sheet.Cells.DeleteRows(7, 5); | |
// Save the excel file. | |
workbook.Save(dataDir+ "out_book1.out.xlsx"); |