Aspose.Cellsアプリケーションの最初の例 Hello World
Contents
[
Hide
]
このチュートリアルでは、Aspose.CellsのシンプルなAPIを使用して、非常に基本的なアプリケーション(Hello World)を作成する方法を示しています。このシンプルなアプリケーションでは、指定されたワークシートのセルに ‘Hello World’ というテキストが含まれるMicrosoft Excelファイルが作成されます。
Hello Worldアプリケーションの作成方法
以下の手順により、Aspose.Cells APIを使用してHello Worldアプリケーションを作成できます:
- Workbookクラスのインスタンスを作成します。
- ライセンスがある場合は、適用します。 評価版を使用している場合は、ライセンスに関連するコード行をスキップします。 1.新しいExcelファイルを作成するか、既存のExcelファイルを開きます。
- Excelファイルのワークシートの任意のセルにアクセスします。
- アクセスしたセルに**Hello World!**の単語を挿入します。
- 変更されたMicrosoft Excelファイルを生成します。
上記の手順の実装は、以下の例で示されています。
新しいワークブックの作成方法
次の例は、ゼロから新しいワークブックを作成し、最初のワークシートのセルA1にHello World!を書き込み、Excelファイルを保存します。
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
try | |
{ | |
// Create a License object | |
License license = new License(); | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.SetLicense(dataDir + "Aspose.Cells.lic"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
// Instantiate a Workbook object that represents Excel file. | |
Workbook wb = new Workbook(); | |
// When you create a new workbook, a default "Sheet1" is added to the workbook. | |
Worksheet sheet = wb.Worksheets[0]; | |
// Access the "A1" cell in the sheet. | |
Cell cell = sheet.Cells["A1"]; | |
// Input the "Hello World!" text into the "A1" cell | |
cell.PutValue("Hello World!"); | |
// Save the Excel file. | |
wb.Save(dataDir + "MyBook_out.xlsx"); |
既存のファイルを開く方法
次の例は、存在するMicrosoft Excelテンプレートファイル「Sample.xlsx」を開き、最初のワークシートの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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
try | |
{ | |
// Create a License object | |
License license = new License(); | |
// Set the license of Aspose.Cells to avoid the evaluation limitations | |
license.SetLicense("Aspose.Cells.lic"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Sample.xlsx", FileMode.Open); | |
// Instantiate a Workbook object that represents the existing Excel file | |
Workbook workbook = new Workbook(fstream); | |
// Get the reference of "A1" cell from the cells collection of a worksheet | |
Cell cell = workbook.Worksheets[0].Cells["A1"]; | |
// Put the "Hello World!" text into the "A1" cell | |
cell.PutValue("Hello World!"); | |
// Save the Excel file | |
workbook.Save(dataDir + "HelloWorld_out.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |