Ihre erste Aspose.Cells Bewerbung - Hello World

Erstellen der Hello World-Anwendung

Die folgenden Schritte erstellen die Hello World-Anwendung unter Verwendung der Aspose.Cells API:

  1. Erstellen Sie eine Instanz derArbeitsmappe Klasse.
  2. Wenn Sie eine Lizenz haben, dannWende es an. Wenn Sie die Evaluierungsversion verwenden, überspringen Sie die lizenzbezogenen Codezeilen.
  3. Erstellen Sie eine neue Excel-Datei oder öffnen Sie eine vorhandene Excel-Datei.
  4. Greifen Sie in der Excel-Datei auf eine beliebige Zelle eines Arbeitsblatts zu.
  5. Füge die Wörter einHello World! in eine zugegriffene Zelle.
  6. 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.

// 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.

// 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();