Ihre erste Aspose.Cells Bewerbung - Hello World
Contents
[
Hide
]
Dieses Tutorial zeigt, wie Sie eine allererste Anwendung (Hello World) mit Aspose.Cells' einfach API erstellen. Diese einfache Anwendung erstellt eine Microsoft Excel-Datei mit dem Text ‘Hello World’ in einer bestimmten Arbeitsblattzelle.
Erstellen der Hello World-Anwendung
Die folgenden Schritte erstellen die Hello World-Anwendung unter Verwendung der Aspose.Cells API:
- Erstellen Sie eine Instanz derArbeitsmappe Klasse.
- Wenn Sie eine Lizenz haben, dannWende es an. Wenn Sie die Evaluierungsversion verwenden, überspringen Sie die lizenzbezogenen Codezeilen.
- Erstellen Sie eine neue Excel-Datei oder öffnen Sie eine vorhandene Excel-Datei.
- Greifen Sie in der Excel-Datei auf eine beliebige Zelle eines Arbeitsblatts zu.
- Füge die Wörter einHello World! in eine zugegriffene Zelle.
- Generieren Sie die geänderte Excel-Datei Microsoft.
Die Implementierung der obigen Schritte wird in den folgenden Beispielen demonstriert.
Codebeispiel: Erstellen einer neuen Arbeitsmappe
Das folgende Beispiel erstellt eine neue Arbeitsmappe von Grund auf neu, schreibt Hello World! in Zelle A1 auf dem ersten Arbeitsblatt und speichert die Excel-Datei.
This file contains hidden or 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"); |
Codebeispiel: Öffnen einer vorhandenen Datei
Das folgende Beispiel öffnet eine vorhandene Excel-Vorlagendatei Microsoft mit dem Namen “Sample.xlsx”, gibt “Hello World!” Text in die Zelle A1 im ersten Arbeitsblatt und speichert die Arbeitsmappe.
This file contains hidden or 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(); |