Votre première application Aspose.Cells Hello World

Création de l’application Hello World

Pour créer l’application Hello World à l’aide de l’API Aspose.Cells :

  1. Créez une instance de la classe Workbook.
  2. Appliquer la licence :
    1. Si vous avez acheté une licence, utilisez-la dans votre application pour accéder à toutes les fonctionnalités d’Aspose.Cells.
    2. Si vous utilisez la version d’évaluation du composant (si vous utilisez Aspose.Cells sans licence), ignorez cette étape.
  3. Créez un nouveau fichier Microsoft Excel, ou ouvrez un fichier existant dans lequel vous souhaitez ajouter ou mettre à jour du texte.
  4. Accédez à n’importe quelle cellule d’une feuille de calcul du fichier Microsoft Excel.
  5. Insérez les mots Bonjour le Monde ! dans une cellule accessible.
  6. Générez le fichier Microsoft Excel modifié.

Les exemples ci-dessous démontrent les étapes ci-dessus.

Création d’un classeur

L’exemple suivant crée un nouveau classeur à partir de zéro, écrit les mots “Bonjour le Monde !” dans la cellule A1 sur la première feuille de calcul, et sauvegarde le fichier.

La feuille de calcul générée

todo:image_alt_text

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

Ouverture d’un fichier existant

L’exemple suivant ouvre un fichier modèle Microsoft Excel existant appelé book1.xls, écrit les mots “Bonjour le Monde !” dans la cellule A1 de la première feuille de calcul, et enregistre le classeur sous un nouveau nom de fichier.

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