管理工作表
使用 Aspose.Cells 管理工作表就像 ABC 一样简单。在本节中,我们将描述我们如何:
- 从头开始创建一个新的 Excel 文件并向其中添加一个工作表
- 将工作表添加到设计器电子表格
- 使用工作表名称访问工作表
- 使用工作表名称从 Excel 文件中删除工作表
- 使用工作表索引从 Excel 文件中删除工作表
Aspose.Cells提供了一个类,工作簿表示一个 Excel 文件。工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。
工作表由工作表班级。工作表类提供了广泛的属性和方法来管理工作表。让我们看看如何使用这些基本的 API 集。
将工作表添加到新的 Excel 文件
要以编程方式创建新的 Excel 文件,开发人员需要创建一个对象工作簿表示 Excel 文件的类。然后开发者可以调用[添加](https://reference.aspose.com/cells/java/com.aspose.cells/worksheetcollection#add() 的方法工作表集合.当我们打电话添加方法,会自动在Excel文件中添加一个空工作表,可以通过将新添加的工作表的工作表索引传递给工作表集合.获得工作表引用后,开发人员可以根据自己的需求对工作表进行处理。在工作表上完成工作后,开发人员可以通过调用[救球](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#save(java.io.OutputStream,%20com.aspose.cells.SaveOptions) 的方法工作簿班级。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AddingWorksheetstoNewExcelFile.class) + "worksheets/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
int sheetIndex = worksheets.add(); | |
Worksheet worksheet = worksheets.get(sheetIndex); | |
// Setting the name of the newly added worksheet | |
worksheet.setName("My Worksheet"); | |
// Saving the Excel file | |
workbook.save(dataDir + "AWToNewExcelFile_out.xls"); | |
// Print Message | |
System.out.println("Sheet added successfully."); |
将工作表添加到 Designer 电子表格
将工作表添加到设计器电子表格的过程与上述方法完全相同,只是 Excel 文件已经创建,我们需要先打开该 Excel 文件,然后再向其添加工作表。可以通过在初始化时传递文件路径或流来打开设计器电子表格工作簿班级。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AddingWorksheetstoDesignerSpreadsheet.class) + "worksheets/"; | |
// Creating a file stream containing the Excel file to be opened | |
FileInputStream fstream = new FileInputStream(dataDir + "book.xls"); | |
// Instantiating a Workbook object with the stream | |
Workbook workbook = new Workbook(fstream); | |
// Adding a new worksheet to the Workbook object | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
int sheetIndex = worksheets.add(); | |
Worksheet worksheet = worksheets.get(sheetIndex); | |
// Setting the name of the newly added worksheet | |
worksheet.setName("My Worksheet"); | |
// Saving the Excel file | |
workbook.save(dataDir + "AWToDesignerSpreadsheet_out.xls"); | |
// Closing the file stream to free all resources | |
fstream.close(); | |
// Print Message | |
System.out.println("Sheet added successfully."); |
使用工作表名称访问工作表
开发人员可以通过指定其名称或索引来访问或获取任何工作表。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(AccessingWorksheetsusingSheetName.class) + "worksheets/"; | |
String filePath = dataDir + "book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileInputStream fstream = new FileInputStream(filePath); | |
// Instantiating a Workbook object with the stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing a worksheet using its sheet name | |
Worksheet worksheet = workbook.getWorksheets().get("Sheet1"); | |
Cell cell = worksheet.getCells().get(0, 0); | |
// Print Message | |
System.out.println(cell.getValue()); |
使用工作表名称删除工作表
有时,开发人员可能需要从现有 Excel 文件中删除工作表,可以通过调用[移除点](https://reference.aspose.com/cells/java/com.aspose.cells/worksheetcollection#removeAt(java.lang.String) 的方法工作表集合收藏。我们可以将工作表名称传递给[移除点](https://reference.aspose.com/cells/java/com.aspose.cells/worksheetcollection#removeAt(java.lang.String)方法删除特定工作表。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(RemovingWorksheetsusingSheetName.class) + "worksheets/"; | |
// Creating a file stream containing the Excel file to be opened | |
FileInputStream fstream = new FileInputStream(dataDir + "book.xls"); | |
// Instantiating a Workbook object with the stream | |
Workbook workbook = new Workbook(fstream); | |
// Removing a worksheet using its sheet name | |
workbook.getWorksheets().removeAt("Sheet1"); | |
// Saving the Excel file | |
workbook.save(dataDir + "RemovingWorksheetsusingSheetName_out.xls"); | |
// Closing the file stream to free all resources | |
fstream.close(); | |
// Print Message | |
System.out.println("Sheet removed successfully."); |
使用工作表索引删除工作表
如果开发人员已经知道要删除的工作表的工作表名称,则上述删除工作表的方法很有效。但是,如果您不知道要从 Excel 文件中删除的工作表的工作表名称怎么办?
那么,在这种情况下,开发人员可以使用重载版本[移除点](https://reference.aspose.com/cells/java/com.aspose.cells/worksheetcollection#removeAt(int)方法,它采用工作表的工作表索引而不是其工作表名称。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(RemovingWorksheetsusingSheetIndex.class) + "worksheets/"; | |
// Creating a file stream containing the Excel file to be opened | |
FileInputStream fstream = new FileInputStream(dataDir + "book.xls"); | |
// Instantiating a Workbook object with the stream | |
Workbook workbook = new Workbook(fstream); | |
// Removing a worksheet using its sheet index | |
workbook.getWorksheets().removeAt(0); | |
// Saving the Excel file | |
workbook.save(dataDir + "RWUsingSheetIndex_out.xls"); | |
// Closing the file stream to free all resources | |
fstream.close(); | |
// Print Message | |
System.out.println("Sheet removed successfully."); |