最初の Aspose.Cells アプリケーション - Hello World
Contents
[
Hide
]
この初心者向けトピックでは、開発者が Aspose.Cells' simple API を使用して単純な最初のアプリケーション (Hello World) を作成する方法を示します。アプリケーションは、ワークシートの指定されたセルに Hello World という単語を含む Microsoft Excel ファイルを作成します。
Hello World アプリケーションの作成
Aspose.Cells API を使用して Hello World アプリケーションを作成するには:
- のインスタンスを作成します**ワークブック**クラス。
- ライセンスを適用します。
- ライセンスを購入した場合は、アプリケーションでライセンスを使用して Aspose.Cells の全機能にアクセスします
- コンポーネントの評価版を使用している場合 (ライセンスなしで Aspose.Cells を使用している場合)、この手順をスキップします。
- 新しい Microsoft Excel ファイルを作成するか、テキストを追加/更新する既存のファイルを開きます。
- Microsoft Excel ファイルのワークシートの任意のセルにアクセスします。
- 単語を挿入する**Hello World!**アクセスされたセルに。
- 変更された Microsoft Excel ファイルを生成します。
以下の例は、上記の手順を示しています。
ワークブックの作成
次の例では、新しいワークブックをゼロから作成し、“Hello World!” という単語を書き込みます。最初のワークシートのセル A1 に入力し、ファイルを保存します。
生成されたスプレッドシート
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"); |
既存のファイルを開く
次の例では、既存の Microsoft Excel テンプレート ファイルを開きます。book1.xls、「Hello World!」という言葉を書きます。最初のワークシートのセル A1 で、ブックを新しいファイルとして保存します。
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"); |