管理 Microsoft Excel 文件的工作表。

Aspose.Cells提供了一个类,工作簿表示一个 Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。

工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。

将工作表添加到新的 Excel 文件

要以编程方式创建新的 Excel 文件:

  1. 创建对象的工作簿班级。
  2. 打电话给添加的方法工作表集合班级。一个空工作表会自动添加到 Excel 文件中。可以通过将新工作表的工作表索引传递给工作表收藏。
  3. 获取工作表参考。
  4. 在工作表上执行工作。
  5. 通过调用工作簿班级'救球方法。
// 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");

将工作表添加到 Designer 电子表格

将工作表添加到设计器电子表格的过程与添加新工作表的过程相同,只是 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);
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);

使用工作表名称删除工作表

要从文件中删除工作表,请调用移除位置的方法工作表集合班级。将工作表名称传递给移除位置删除特定工作表的方法。

// 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");

使用工作表索引删除工作表

当工作表的名称已知时,按名称删除工作表效果很好。如果您不知道工作表的名称,请使用移除位置采用工作表的工作表索引而不是其工作表名称的方法。

// 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");

激活工作表并在工作表中激活 Cell

有时,当用户在 Excel 中打开 Microsoft Excel 文件时,您需要激活并显示特定的工作表。同样,您可能想要激活特定单元格并设置滚动条以显示活动单元格。 Aspose.Cells 能够完成所有这些任务。

一个活动表是您正在处理的工作表:选项卡上的活动工作表名称默认为粗体。

一个活性细胞是一个选定的单元格,当您开始键入时,数据将被输入到该单元格中。一次只有一个细胞处于活动状态。活动单元格以粗边框突出显示。

激活工作表并激活 Cell

Aspose.Cells 提供用于激活工作表和单元格的特定 API 调用。例如,Aspose.Cells.WorksheetCollection.ActiveSheetIndex属性对于在工作簿中设置活动工作表很有用。 相似地,Aspose.Cells.Worksheet.ActiveCell属性用于设置和获取工作表中的活动单元格。

要确保水平或垂直滚动条位于要显示特定数据的行和列索引位置,请使用Aspose.Cells.Worksheet.FirstVisibleRowAspose.Cells.Worksheet.FirstVisibleColumn特性。

以下示例显示如何激活工作表并在其中创建活动单元格。在生成的输出中,滚动条将滚动以使第 2 行和第 2 列成为它们的第一个可见行和列。

// 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");

推进主题