您的第一个 Aspose.Cells 应用程序 Hello World
Contents
[
Hide
]
这个初学者主题展示了开发人员如何使用 Aspose.Cells 的简单 API 创建一个简单的第一个应用程序(Hello World)。该应用程序在工作表的指定单元格中创建了一个包含 Hello World 文字的 Microsoft Excel 文件。
创建Hello World应用程序
使用 Aspose.Cells API 创建 Hello World 应用程序:
- 创建 Workbook 类的一个实例。
- 应用许可证:
- 如果您已购买许可证,请在应用程序中使用该许可证以获得对 Aspose.Cells 全部功能的访问。
- 如果您正在使用组件的评估版本 (如果您在没有许可证的情况下使用 Aspose.Cells),请跳过此步骤。
- 创建一个新的 Microsoft Excel 文件,或者打开现有文件,在其中您想要添加/更新一些文本。
- 访问 Microsoft Excel 文件中的工作表的任何单元格。
- 在访问的单元格中插入单词Hello World!。
- 生成修改后的Microsoft Excel文件。
下面的示例演示了上述步骤。
创建一个工作簿
以下示例从头开始创建一个新的工作簿,在第一个工作表的单元格A1中写入单词"Hello World!",然后保存文件。
生成的电子表格
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreatingWorkbook.class); | |
// Creating a file input stream to reference the license file | |
FileInputStream fstream = new FileInputStream("Aspose.Cells.lic"); | |
// Create a License object | |
License license = new License(); | |
// Applying the Aspose.Cells license | |
license.setLicense(fstream); | |
// Instantiating a Workbook object that represents a Microsoft Excel | |
// file. | |
Workbook wb = new Workbook(); | |
// Note when you create a new workbook, a default worksheet, "Sheet1", is by default added to the workbook. Accessing the | |
// first worksheet in the book ("Sheet1"). | |
Worksheet sheet = wb.getWorksheets().get(0); | |
// Access cell "A1" in the sheet. | |
Cell cell = sheet.getCells().get("A1"); | |
// Input the "Hello World!" text into the "A1" cell | |
cell.setValue("Hello World!"); | |
// Save the Microsoft Excel file. | |
wb.save(dataDir + "MyBook.xls", FileFormatType.EXCEL_97_TO_2003); | |
wb.save(dataDir + "MyBook.xlsx"); | |
wb.save(dataDir + "MyBook.ods"); |
打开现有文件
以下示例打开名为book1.xls的现有Microsoft Excel模板文件,在第一个工作表的单元格A1中写入单词"Hello World!",然后将工作簿保存为新文件。
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(OpeningExistingFile.class); | |
// Creating a file input stream to reference the license file | |
FileInputStream fstream = new FileInputStream("Aspose.Cells.lic"); | |
// Create a License object | |
License license = new License(); | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.setLicense(fstream); | |
// Instantiate a Workbook object that represents an Excel file | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the reference of "A1" cell from the cells of a worksheet | |
Cell cell = workbook.getWorksheets().get(0).getCells().get("A1"); | |
// Set the "Hello World!" value into the "A1" cell | |
cell.setValue("Hello World!"); | |
// Write the Excel file | |
workbook.save(dataDir + "HelloWorld.xls", FileFormatType.EXCEL_97_TO_2003); | |
workbook.save(dataDir + "HelloWorld.xlsx"); | |
workbook.save(dataDir + "HelloWorld.ods"); |