管理Microsoft Excel文件的工作表
Aspose.Cells提供了一个表示Excel文件的类,Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。
工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法来管理工作表。
向新的Excel文件添加工作表
要通过程序创建新的Excel文件:
- 创建Workbook类的对象。
- 调用 WorksheetCollection 类的 Add 方法。自动向 Excel 文件中添加一个空工作表。可以通过将新工作表的索引传递给 Worksheets 集合来引用它。
- 获取工作表引用。
- 对工作表进行操作。
- 调用 Workbook 类的 Save 方法,保存带有新工作表的新 Excel 文件。
// 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); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Setting the name of the newly added worksheet | |
worksheet.Name = "My Worksheet"; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
向设计电子表格添加工作表
向设计电子表格添加工作表的过程与添加新工作表的过程相同,只是 Excel 文件已经存在,所以在添加工作表之前应该打开它。可以通过 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. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Adding a new worksheet to the Workbook object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Setting the name of the newly added worksheet | |
worksheet.Name = "My Worksheet"; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xlsx"); |
使用工作表名称访问工作表
通过指定名称或索引来访问任何工作表。
// 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); | |
string InputPath = dataDir + "book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing a worksheet using its sheet name | |
Worksheet worksheet = workbook.Worksheets["Sheet1"]; | |
Cell cell = worksheet.Cells["A1"]; | |
Console.WriteLine(cell.Value); |
使用工作表名称移除工作表
要从文件中删除工作表,调用 WorksheetCollection 类的 RemoveAt 方法。将工作表的名称传递给 RemoveAt 方法以删除特定工作表。
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Removing a worksheet using its sheet name | |
workbook.Worksheets.RemoveAt("Sheet1"); | |
// Save workbook | |
workbook.Save(dataDir + "output.out.xls"); |
通过页索引删除工作表
当知道工作表的名称时,通过名称删除工作表很方便。如果不知道工作表的名称,则可以使用重载版本的 RemoveAt 方法,该方法接受工作表的索引而不是工作表名称。
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Removing a worksheet using its sheet index | |
workbook.Worksheets.RemoveAt(0); | |
// Save workbook | |
workbook.Save(dataDir + "output.out.xls"); |
激活工作表并使工作表中的单元格处于活动状态
有时,您需要让特定工作表在用户在 Excel 中打开 Microsoft Excel 文件时处于活动状态并显示。同样,您可能希望激活特定单元格并设置滚动条以显示活动单元格。 Aspose.Cells 能够执行所有这些任务。
活动工作表 是您正在处理的工作表:标签上的活动工作表名称默认为粗体。
活动单元格 是所选单元格,也就是在开始输入数据时输入数据的单元格。一次只有一个单元格处于活动状态。活动单元格由粗边框突出显示。
激活工作表并使单元格处于活动状态
Aspose.Cells 提供特定的 API 调用来激活工作表和单元格。例如,Aspose.Cells.WorksheetCollection.ActiveSheetIndex 属性可用于在工作簿中设置活动工作表。 类似地,Aspose.Cells.Worksheet.ActiveCell 属性用于设置和获取工作表中的活动单元格。
要确保水平或垂直滚动条位于要显示特定数据的行和列索引位置,请使用 Aspose.Cells.Worksheet.FirstVisibleRow 和 Aspose.Cells.Worksheet.FirstVisibleColumn 属性。
以下示例显示了如何激活工作表并将其中一个单元格设为活动单元格。在生成的输出中,滚动条将滚动以使第二行和第二列成为它们的第一个可见行和列。
// 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 new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the workbook. | |
Worksheet worksheet1 = workbook.Worksheets[0]; | |
// Get the cells in the worksheet. | |
Cells cells = worksheet1.Cells; | |
// Input data into B2 cell. | |
cells[1, 1].PutValue("Hello World!"); | |
// Set the first sheet as an active sheet. | |
workbook.Worksheets.ActiveSheetIndex = 0; | |
// Set B2 cell as an active cell in the worksheet. | |
worksheet1.ActiveCell = "B2"; | |
// Set the B column as the first visible column in the worksheet. | |
worksheet1.FirstVisibleColumn = 1; | |
// Set the 2nd row as the first visible row in the worksheet. | |
worksheet1.FirstVisibleRow = 1; | |
// Save the excel file. | |
workbook.Save(dataDir + "output.xls"); | |