插入或删除行或列
Contents
[
Hide
]
无论是从头开始创建新工作表还是在现有工作表上进行操作,我们可能需要在工作表中添加额外行或列以容纳更多数据或出于其他原因。反之,可能还需要从工作表的指定位置删除行或列。
管理行/列
Aspose.Cells提供了一个表示Excel文件的类Workbook。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合,表示工作表中的所有单元格。
Cells集合提供了几种管理工作表中行或列的方法,下面详细讨论了其中的一些。
插入行
开发人员可以通过调用Cells集合的InsertRow方法在任何位置向工作表中插入一行。InsertRow方法需要插入新行的行索引。
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Inserting a row into the worksheet at 3rd position
worksheet.Cells.InsertRow(2);
//Saving the modified Excel file
workbook.Save(MyDir + "Inserting Row.xls");
//Closing the file stream to free all resources
fstream.Close();
插入多行
有时,开发人员可能需要在工作表中插入多行。这可以通过调用Cells集合的InsertRows方法来实现。InsertRows方法接受两个参数:
- 行索引,新行将被插入的行的索引
- 行数,需要插入的总行数
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Inserting 10 rows into the worksheet starting from 3rd row
worksheet.Cells.InsertRows(2, 10);
//Saving the modified Excel file
workbook.Save(MyDir + "Inserting Mutiple Rows.xls");
//Closing the file stream to free all resources
fstream.Close();
删除行
开发人员可以通过调用Cells集合的DeleteRow方法在任何位置删除工作表中的一行。DeleteRow方法需要被删除的行的索引。
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Deleting 3rd row from the worksheet
worksheet.Cells.DeleteRow(2);
//Saving the modified Excel file
workbook.Save(MyDir + "Deleting Rows.xls");
//Closing the file stream to free all resources
fstream.Close();
删除多行
如果开发人员需要从工作表中删除多行,也可以通过调用Cells集合的DeleteRows方法来实现。DeleteRows方法需要两个参数:
- 行索引,将要删除的行的索引
- 行数,需要删除的总行数
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Deleting 10 rows from the worksheet starting from 3rd row
worksheet.Cells.DeleteRows(2, 10);
//Saving the modified Excel file
workbook.Save(MyDir + "Deleting Mutiple Rows.xls");
//Closing the file stream to free all resources
fstream.Close();
插入列
开发人员还可以通过调用Cells集合的InsertColumn方法在任何位置向工作表中插入一列。InsertColumn方法需要插入新列的列索引。
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Inserting a column into the worksheet at 2nd position
worksheet.Cells.InsertColumn(1);
//Saving the modified Excel file
workbook.Save(MyDir + "Inserting Column.xls");
//Closing the file stream to free all resources
fstream.Close();
删除列
要在任何位置删除工作表中的列,开发人员可以调用Cells集合的DeleteColumn方法。DeleteColumn方法需要被删除的列的索引。
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(MyDir + "Row and Column Operation.xls", FileMode.Open);
//Instantiating a Workbook object
//Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
//Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
//Deleting a column from the worksheet at 2nd position
worksheet.Cells.DeleteColumn(1);
//Saving the modified Excel file
workbook.Save(MyDir + "Deleting Column.xls");
//Closing the file stream to free all resources
fstream.Close();